1 / 13

Insertion Sort and Its Analysis

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”

stevie
Download Presentation

Insertion Sort and Its Analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cormen, et al. pp. 17-27 Insertion Sort and Its Analysis

  2. Insertion sort algorithm

  3. Insertion sort pseudocode

  4. 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

  5. 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))

  6. 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

  7. 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

  8. 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!

  9. 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.

  10. Running time analysis

  11. 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

  12. 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

  13. 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

More Related