1. Sorting Arrays (Insertion Sort)
Insertion Sort
Insertion Sort is another algorithm for arranging the elements in a collection (array)
in a particular order. It works by considering the items in the array one at a time.
For each item the algorithm attempts to move it as far left (towards the beginning)
as possible. That is, the current item keeps moving until it encounters another item
that is smaller than it.
An analogy to keep in mind is a line at the airport. Each person in the line will
keep trying to move ahead of the people in front who have later flights.
The above description suggests that the current item keeps swapping with the one
in front of it. Actually, there is no need to swap -- the current item simply vacates
its spot, so that the ones that are bigger than it can keep sliding to the right.
As soon as a smaller item is discovered, the current one moves behind it.
Here is a visualization of the process from Wikipedia:
Here is a visualization of Insertion Sort through a dance:
Here is another example of the execution of Insertion Sort:
original: 7 4 1 8 3 2 6 5 (no need to push left 1st item (7))
<- |
after (4), now pushing (1): 4 7 1 8 3 2 6 5
< - - |
after (1), now pushing (8): 1 4 7 8 3 2 6 5 (no change, 8 stuck behind 7)
.
after (8), now pushing (3): 1 4 7 8 3 2 6 5
<- - - - -|
after (3), now pushing (2): 1 3 4 7 8 2 6 5
< - - - - - -|
after (2), now pushing (6): 1 2 3 4 7 8 6 5
< - - -|
after (6), now pushing (5): 1 2 3 4 6 7 8 5
<- - - - -|
after (5), done: 1 2 3 4 5 6 7 8
Done! Attempted to push to the left each item!
Insertion Sort Implementation
Just as in Bubble Sort it is convenient to use a helper procedure that accomplishes
some work and simply repeat this procedure enough times until the array is sorted.
The helper procedure will be given the index of the item we want to move and will
carry out the actual work of moving this item to the left (actually, it will be
moving the larger ones to the right as the animation above shows).
The helper procedure in this case needs to use a while loop since we keep moving to
the left as long as the current item is smaller than the one before it (and as long
as we have not reached the front).
Insertion Sort Observations
We made the following observations about the execution of Insertion Sort:
1. After the k-th run (i.e. moving the k-th item), the first k items are ordered
(but not necessarily in their final spots yet).
____________________________________ _____________
|__ordered, but not in final places__|__scrambled__|
2. The algorithm requires n-1 runs, i.e. picking each item (except for the
first one) and trying to move it as far to the left as possible.
3. The algorithm does not make "true swaps" as in Bubble Sort. Instead, it makes
"half swaps", since the current item does not slide, but remains on the side
until a spot is vacated for it (see animation above).
Overall, there seem to be some similarities in the way Bubble Sort and Insertion
Sort operate, but it is likely that Insertion Sort will be faster than Bubble
Sort since the swaps in Insertion Sort are "simpler".