Title: Insertion Sort and Its Analysis
1Insertion Sort and Its Analysis
2Insertion sort algorithm
3Insertion sort pseudocode
4Loop invariants and correctness of algorithms
- Iterative algorithms can often be proven correct
by means of a loop invariant - A loop invariant is a parameterized statement
that is true at the beginning of every loop
5Using a loop invariant
- You show that the loop invariant is true at the
beginning, before the first looping
(initialization or base case) - Then show that if the loop invariant is true just
before the kth looping, then it will true just
before the (k1)th looping as well (maintenance
or induction case) - Must also show that if the loop invariant is true
after the last looping, then the algorithm is
correct (termination, this is whats new compared
to the usual mathematical induction on an
infinite set (positive integers))
6Loop invariant for insertion sort
- The index j indicates the current card being
inserted into the hand of already sorted cards - At the beginning of each iteration of the outer
for loop, indexed by j, the subarray A1..j-1
constitute the sorted hand - Elements Aj1..n correspond to the pile of
cards still on the table - In fact, elements A1..j-1 are the elements
originally in positions 1 through j-1, but now in
sorted order -- this is to be stated formally as
the loop invariant
7Loop invariant
At the start of each iteration of the for loop of
lines 1-8, the subarray A1..j-1 consists of the
elements originally in A1..j-1 but in sorted
order
8Showing Step 1 that the loop invariant is true
before the first iteration
Trivially true, because 1. A1..1 contains the
element that was originally there because we
havent moved any data, and, 2. A1..1 is in
sorted order because theres only one element!
9Showing Step 2 showing that if the loop
invariant is true before iteration k, then it is
also true before iteration k1
Assume Antecedent Before iteration k, A1..k
consists of elements originally in A1..k, in
sorted (non-descending) order.
To be proven After iteration k, or before
iteration k1, A1..k1 consists of elements
originally in A1..k1, in sorted order.
Proof during iteration k, we take the key, or
element number k1 (which is j) and insert it in
between the elements that are smaller than the
key and the elements that are greater than the
key. In so doing, we move the elements greater
than the key one place to the right without
disturbing their relative positions and without
creating any holes in between these elements.
Therefore A1..k1 is sorted after then kth
iteration.
10Running time analysis
11Some explanation
- times column refers to how many times each stmt
is executed (max) - n lengthA
- tj times the while loop test in line 5 is
executed for that value of the index j
12Best case analysis
T(n) c1n c2(n-1) c4(n-1) c5(n-1) c8(n-1)
T(n) is a linear function of n
13Worst-case analysis(array reverse-sorted)
Must compare each element Aj w/ each element in
the entire sorted subarray A1..j-1
So T(j) j, for j 2,3,,n
Thus, T(n) c1n c2(n-1) c4(n-1)
c5(n(n1)/2 1) c6(n(n-1)/2) c7(n(n-1)/2)
c8(n-1)
T(n) is a quadratic function of n