Algorithm Search⌘K

Proofs: Insertion sort

These are the human proofs of Insertion sort, at full length.

Insertion sort sorts the array and keeps its items full

This proof shows: correctness.

Let A0A_0 be the array at the start. We prove the invariant of the outer loop:

(I) Before the step for ii, the part A[0…i−1]A[0 \ldots i-1] is a permutation of A0[0…i−1]A_0[0 \ldots i-1] in non-decreasing order, and A[i…n−1]=A0[i…n−1]A[i \ldots n-1] = A_0[i \ldots n-1].

Start. For i=1i = 1, the part A[0…0]A[0 \ldots 0] has one item, so it is sorted. No item has moved yet.

Step. Assume (I) before the step for ii. Line L2 sets key=A[i]=A0[i]key = A[i] = A_0[i]. For the inner loop we use this invariant:

(J) −1≤j≤i−1-1 \le j \le i-1. The part A[0…j]A[0 \ldots j] is not changed. The part A[j+2…i]A[j+2 \ldots i] is the old A[j+1…i−1]A[j+1 \ldots i-1], moved one place right. Each item of A[j+2…i]A[j+2 \ldots i] is larger than keykey.

Before the first test of line L4, j=i−1j = i - 1 and A[j+2…i]A[j+2 \ldots i] is empty, so (J) is true. If the test of line L4 is true, then A[j]>keyA[j] > key. Line L5 copies A[j]A[j] to A[j+1]A[j+1], and line L6 makes jj one less. So (J) is true again. The value jj becomes smaller in each step and j≥−1j \ge -1, so the inner loop stops.

When the inner loop stops, j=−1j = -1 or A[j]≤keyA[j] \le key. Line L7 puts keykey at A[j+1]A[j+1]. Now:

  • each item of A[0…j]A[0 \ldots j] is at most keykey, because the old part was sorted and A[j]≤keyA[j] \le key, or the part is empty;
  • A[j+1]=keyA[j+1] = key;
  • each item of A[j+2…i]A[j+2 \ldots i] is larger than keykey, by (J), and these items are in the old order.

So A[0…i]A[0 \ldots i] is sorted. It holds the old A[0…i−1]A[0 \ldots i-1] and A0[i]A_0[i], so it is a permutation of A0[0…i]A_0[0 \ldots i]. The items after ii did not change. This is (I) for i+1i + 1.

End. The loop stops with i=ni = n. By (I), A[0…n−1]A[0 \ldots n-1] is a permutation of A0A_0 in non-decreasing order. ■\blacksquare

Stability. An item moves right in line L5 only past an item that is larger than keykey. So keykey never moves before an item that is equal to it and that was before it in A0A_0.

Back to Insertion sort