|
1
|
- Chapter 12 presents several common algorithms for sorting an array of
integers.
- Two slow but simple algorithms are Selectionsort and Insertionsort.
- This presentation demonstrates how the two algorithms work.
|
|
2
|
- The picture shows an array of six integers that we want to sort from
smallest to largest
|
|
3
|
- Start by finding the smallest entry.
|
|
4
|
- Start by finding the smallest entry.
- Swap the smallest entry with the first entry.
|
|
5
|
- Start by finding the smallest entry.
- Swap the smallest entry with the first entry.
|
|
6
|
- Part of the array is now sorted.
|
|
7
|
- Find the smallest element in the unsorted side.
|
|
8
|
- Find the smallest element in the unsorted side.
- Swap with the front of the unsorted side.
|
|
9
|
- We have increased the size of the sorted side by one element.
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
- The process keeps adding one more number to the sorted side.
- The sorted side has the smallest numbers, arranged from small to large.
|
|
14
|
- We can stop when the unsorted side has just one number, since that
number must be the largest number.
|
|
15
|
- The array is now sorted.
- We repeatedly selected the smallest element, and moved this element to
the front of the unsorted side.
|
|
16
|
- The Insertionsort algorithm also views the array as having a sorted side
and an unsorted side.
|
|
17
|
- The sorted side starts with just the first element, which is not
necessarily the smallest element.
|
|
18
|
- The sorted side grows by taking the front element from the unsorted
side...
|
|
19
|
- ...and inserting it in the place that keeps the sorted side arranged
from small to large.
|
|
20
|
- In this example, the new element goes in front of the element that was
already in the sorted side.
|
|
21
|
- Sometimes we are lucky and the new inserted item doesn't need to move at
all.
|
|
22
|
- Sometimes we are lucky twice in a row.
|
|
23
|
- Copy the new element to a separate location.
|
|
24
|
- Shift elements in the sorted side, creating an open space for the new
element.
|
|
25
|
- Shift elements in the sorted side, creating an open space for the new
element.
|
|
26
|
- Continue shifting elements...
|
|
27
|
- Continue shifting elements...
|
|
28
|
- ...until you reach the location for the new element.
|
|
29
|
- Copy the new element back into the array, at the correct location.
|
|
30
|
- The last element must also be inserted. Start by copying it...
|
|
31
|
- How many shifts will occur before we copy this element back into the
array?
|
|
32
|
|
|
33
|
- Four items are shifted.
- And then the element is copied back into the array.
|
|
34
|
- Both Selectionsort and Insertionsort have a worst-case time of O(n2),
making them impractical for large arrays.
- But they are easy to program, easy to debug.
- Insertionsort also has good performance when the array is nearly sorted
to begin with.
- But more sophisticated sorting algorithms are needed when good
performance is needed in all cases for large arrays.
|
|
35
|
|