3.2 insertion sort
Transcript of 3.2 insertion sort
![Page 1: 3.2 insertion sort](https://reader036.fdocuments.in/reader036/viewer/2022071904/55c5d6d9bb61eb0c5e8b45aa/html5/thumbnails/1.jpg)
Insertion Sort
![Page 2: 3.2 insertion sort](https://reader036.fdocuments.in/reader036/viewer/2022071904/55c5d6d9bb61eb0c5e8b45aa/html5/thumbnails/2.jpg)
Insertion Sort: Idea
Idea: sorting cards. 8 | 5 9 2 6 3 5 8 | 9 2 6 3 5 8 9 | 2 6 3 2 5 8 9 | 6 3 2 5 6 8 9 | 3 2 3 5 6 8 9 |
![Page 3: 3.2 insertion sort](https://reader036.fdocuments.in/reader036/viewer/2022071904/55c5d6d9bb61eb0c5e8b45aa/html5/thumbnails/3.jpg)
Insertion Sort: Idea
1. We have two group of items: sorted group, and unsorted group
2. Initially, all items in the unsorted group and the sorted group is empty. We assume that items in the unsorted
group unsorted. We have to keep items in the sorted group
sorted.
3. Pick any item from, then insert the item at the right position in the sorted group to maintain sorted property.
4. Repeat the process until the unsorted group becomes empty.
![Page 4: 3.2 insertion sort](https://reader036.fdocuments.in/reader036/viewer/2022071904/55c5d6d9bb61eb0c5e8b45aa/html5/thumbnails/4.jpg)
40 2 1 43 3 65 0 -1 58 3 42 4
2 40 1 43 3 65 0 -1 58 3 42 4
1 2 40 43 3 65 0 -1 58 3 42 4
40
Insertion Sort: Example
![Page 5: 3.2 insertion sort](https://reader036.fdocuments.in/reader036/viewer/2022071904/55c5d6d9bb61eb0c5e8b45aa/html5/thumbnails/5.jpg)
1 2 3 40 43 65 0 -1 58 3 42 4
1 2 40 43 3 65 0 -1 58 3 42 4
1 2 3 40 43 65 0 -1 58 3 42 4
Insertion Sort: Example
![Page 6: 3.2 insertion sort](https://reader036.fdocuments.in/reader036/viewer/2022071904/55c5d6d9bb61eb0c5e8b45aa/html5/thumbnails/6.jpg)
1 2 3 40 43 65 0 -1 58 3 42 4
1 2 3 40 43 650 -1 58 3 42 4
1 2 3 40 43 650 58 3 42 41 2 3 40 43 650-1
Insertion Sort: Example
![Page 7: 3.2 insertion sort](https://reader036.fdocuments.in/reader036/viewer/2022071904/55c5d6d9bb61eb0c5e8b45aa/html5/thumbnails/7.jpg)
1 2 3 40 43 650 58 3 42 41 2 3 40 43 650-1
1 2 3 40 43 650 58 42 41 2 3 3 43 650-1 5840 43 65
1 2 3 40 43 650 42 41 2 3 3 43 650-1 5840 43 65
Insertion Sort: Example
1 2 3 40 43 650 421 2 3 3 43 650-1 584 43 6542 5840 43 65
![Page 8: 3.2 insertion sort](https://reader036.fdocuments.in/reader036/viewer/2022071904/55c5d6d9bb61eb0c5e8b45aa/html5/thumbnails/8.jpg)
Insertion Sort: Analysis
Running time analysis: Worst case: O(N2) Best case: O(N)
![Page 9: 3.2 insertion sort](https://reader036.fdocuments.in/reader036/viewer/2022071904/55c5d6d9bb61eb0c5e8b45aa/html5/thumbnails/9.jpg)
A Lower Bound
Bubble Sort, Selection Sort, Insertion Sort all have worst case of O(N2).
Turns out, for any algorithm that exchanges adjacent items, this is the best worst case: Ω(N2)
In other words, this is a lower bound!
![Page 10: 3.2 insertion sort](https://reader036.fdocuments.in/reader036/viewer/2022071904/55c5d6d9bb61eb0c5e8b45aa/html5/thumbnails/10.jpg)
How many squares can you create in this figure by connecting any 4 dots (the corners of a square must lie upon a grid dot?
TRIANGLES:
How many triangles are located in the image below?
![Page 11: 3.2 insertion sort](https://reader036.fdocuments.in/reader036/viewer/2022071904/55c5d6d9bb61eb0c5e8b45aa/html5/thumbnails/11.jpg)
There are 11 squares total; 5 small, 4 medium, and 2 large.
27 triangles. There are 16 one-cell triangles, 7 four-cell triangles, 3 nine-cell triangles, and 1 sixteen-cell triangle.
![Page 12: 3.2 insertion sort](https://reader036.fdocuments.in/reader036/viewer/2022071904/55c5d6d9bb61eb0c5e8b45aa/html5/thumbnails/12.jpg)
GUIDED READING