Analysis of Non-Recursive Insertion Sort


Document last modified: 

Insertion Sort

  • The number of instructions executed by a polynomial time operation is dependent on the size of the problem on which the operation is working.
     
  • The Insertion-Sort is an example of a polynomial time operation. Worst case: O(n2)

Example

loop invariant    At start of each iteration of Line 1: A[1..j-1]  is sorted

Figures a-e: immediately after assignment to j (i.e. darkened)
  1. j=2, A[1..1] is sorted
  2. j=3, A[1..2] is sorted
  3. j=4, A[1..3] is sorted
  4. j=5, A[1..4] is sorted
  5. j=6, A[1..5] is sorted
  6. A[1..6] is sorted

Analysis

tj = # of times the while loop test is executed for value j.

 

Worst and Average case - We usually concentrate on finding the worst-case running time: the longest running time for any input of size n.

Order of growth

Another abstraction to ease analysis and focus on the important features. Look only at the leading term of the formula for running time.

Example

For insertion sort, we already abstracted away the actual statement costs to conclude that the worst-case running time is: an2 + bn + c.

Question 2.22 Which does the graph indicate is more efficient? x*x or x*x+1000x?