150 likes | 1k Views
Cormen, et al. pp. 17-27. Insertion Sort and Its Analysis. Insertion sort algorithm. Insertion sort pseudocode. Loop invariants and correctness of algorithms. Iterative algorithms can often be proven correct by means of a “loop invariant”
E N D
Cormen, et al. pp. 17-27 Insertion Sort and Its Analysis
Loop 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
Using 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 (k+1)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 what’s new compared to the usual mathematical induction on an infinite set (positive integers))
Loop 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 A[1..j-1] constitute the sorted hand • Elements A[j+1..n] correspond to the pile of cards still on the table • In fact, elements A[1..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
Loop invariant At the start of each iteration of the for loop of lines 1-8, the subarray A[1..j-1] consists of the elements originally in A[1..j-1] but in sorted order
Showing Step 1: that the loop invariant is true before the first iteration Trivially true, because 1. A[1..1] contains the element that was originally there because we haven’t moved any data, and, 2. A[1..1] is in sorted order because there’s only one element!
Showing Step 2: showing that if the loop invariant is true before iteration k, then it is also true before iteration k+1 Assume Antecedent: Before iteration k, A[1..k] consists of elements originally in A[1..k], in sorted (non-descending) order. To be proven: After iteration k, or before iteration k+1, A[1..k+1] consists of elements originally in A[1..k+1], in sorted order. Proof: during iteration k, we take the key, or element number k+1 (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 A[1..k+1] is sorted after then kth iteration.
Some explanation • “times” column refers to how many times each stmt is executed (max) • n = length[A] • tj = # times the while loop test in line 5 is executed for that value of the index j
Best 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
Worst-case analysis(array reverse-sorted) Must compare each element A[j] w/ each element in the entire sorted subarray A[1..j-1] So T(j) = j, for j = 2,3,…,n Thus, T(n) = c1n + c2(n-1) + c4(n-1) + c5(n(n+1)/2 – 1) + c6(n(n-1)/2) + c7(n(n-1)/2) + c8(n-1) T(n) is a quadratic function of n