Insertion Sort
Insertion sort is basically an accumulation (recall accumulations from CS 61A?) that is coded in Python as follows:
def sorted(L):
return reduce(sort_insert, L, [])
def sort_insert(x, L):
i = 0
while i < len(L):
if x < L[i]:
break
i += 1
L.insert(i, x)
return L
You are calling sorted on the list L to perform the insertion sort.
Note: If this code isn't fully intuitive, don't worry - it just means that we're doing one insertion at a time while we build up a bunch of stuff that is "done." This is also how you might sort cards by continually putting the next card in the right spot in a group of sorted cards. (You were briefly introduced to this algorithm back when you were learning about invariants.)
Code
Here is code that applies the insertion sort algorithm (to sort into increasing order) to an array of ints.
public static void insertionSort(int[] a) {
for (int p = 1; p < a.length; p++) {
int tmp = a[p];
int j = p;

for(; (j > 0) && (tmp < a[j - 1]); j--) { // Comparison Step
a[j] = a[j - 1];
}
a[j] = tmp;
}
}
Self-test: When is insertion sort good/bad?
Refer to the code above for this self-test. In particular, consider the comparison step in the for loop condition.
Assume we have an array with a million elements, what ordering would minimize the number of comparisons?
Sorted array
Correct! If the array is already sorted, you only do one comparison per iteration of your outer loop. In this case, tmp will be greater than the previous element, so we stop after one comparison.
Reverse sorted array
Incorrect. Think about how many comparisons you need to do to insert the last unsorted element (which is small) into the sorted array, which is smallest to largest.
Randomly sorted array (unsorted and random)
Incorrect. How many comparisons will you need to do on average?
Check Solution
Assume we have an array with a million elements, what ordering would maximize the number of comparisons?
Sorted array
Incorrect. Refer to the answer above to see how sorted arrays would result in the minimal number of comparisons.
Reverse sorted array
Correct!
Randomly sorted array (unsorted and random)
Incorrect. How many comparisons will you need to do on average?
Check Solution
Activity: Write insertion sort applied to linked lists
The file ~cs61bl/code/lab25/IntList.java contains an implementation of a doubly-linked list, along with methods for sorting the list values. Supply the body of the insert method to complete the coding of the insertion sort algorithm.
Insertion sort variation: Tree sort
Insertion sort, in pseudo code is
For each element in the collection to be sorted,
insert it into its proper place in another collection.
The insertion sort algorithm we just considered did its insertion in an array, where elements had to be shifted over to make room for the new elements. Choice of a structure that allows faster insertion -- namedly, a binary search tree -- produces a faster algorithm. We build the tree through repeated insertions, then traverse it in linear time to produce the sorted sequence. This variant of insertion sort is called tree sort.