Design & Analysis of Algorithms: DR Anwar Ghani
Design & Analysis of Algorithms: DR Anwar Ghani
Dr Anwar Ghani
• Output:
5 2 4 6 1 3
sorted unsorted
Insertion Sort
INSERTION-SORT
INSERTION-SORT(A) 1 2 3 4 5 6 7 8
for j ← 2 to n a1 a2 a3 a4 a5 a6 a7 a8
do key ← A[ j ] key
Insert A[ j ] into the sorted sequence A[1 . . j -1]
i←j-1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i←i–1
A[i + 1] ← key
Loop Invariant for Insertion Sort
INSERTION-SORT(A)
for j ← 2 to n
do key ← A[ j ]
Insert A[ j ] into the sorted sequence A[1 . . j -1]
i←j-1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i←i–1
A[i + 1] ← key
Invariant: at the start of the for loop the elements in A[1 . . j-1]
are sorted
Proof
• Proving loop invariants works like induction
• Initialization (base case):
– It is true prior to the first iteration of the loop
• Maintenance (inductive step):
– If it is true before an iteration of the loop, it remains true before
the next iteration
• Termination:
– When the loop terminates, the invariant gives us a useful
property that helps show that the algorithm is correct
– Stop the induction when the loop terminates
Loop Invariant for Insertion Sort
• Initialization:
– Just before the first iteration,
j = 2:
The sub array A[1 . . j-1] = A[1],
The element originally in A[1] is sorted
Loop Invariant for Insertion Sort
• Maintenance:
– the while inner loop moves A[j -1], A[j -2], A[j -
3], and so on, by one position to the right until
the proper position for key is found
– At that point, the value of key is placed into this
position.
T (n) = c1n + c2 (n − 1) + c4 (n − 1) + c5 ∑ t j + c6 ∑ (t j − 1) + c7 ∑ (t j − 1) + c8 ( n − 1)
n n n
j =2 j =2 j =2
Best Case Analysis
• Pre-Sorted Array “while i > 0 and A[i] > key”
– A[i] ≤ key upon the first time the while loop test is run
(when i = j -1)
– tj = 1
= An + B
T (n) = c1n + c2 (n − 1) + c4 (n − 1) + c5 ∑ t j + c6 ∑ (t j − 1) + c7 ∑ (t j − 1) + c8 (n − 1)
n n n
j =2 j =2 j =2
Worst Case Analysis
• Reverse sorted
– Always A[i] > key in while loop test
– Have to compare key with all elements to the left of the jth
position ⇒ compare with j-1 elements ⇒ tj = j
using n
n ( n + 1) n
n ( n + 1) n
n ( n − 1) we have:
∑ j= => ∑ j= −1 => ∑ ( j − 1) =
j =1 2 j=2 2 j=2 2
C = − (C 2 + C 4 + C 5 + C 8 )
Average Case Analysis
INSERTION-SORT(A) cost times
for j ← 2 to n c1 n
do key ← A[ j ] c2 n-1
Insert A[ j ] into the sorted sequence A[1 . . j -1] 0 n-1
do A[i + 1] ← A[i] c6 ∑
n
j =2
( j / 2 − 1)
i ← i – 1 ≈ j/2-1 exchanges c7 ∑
n
j =2
( j / 2 − 1)
n n n n
∑ j =2 j
t = ∑ j =2 j / 2 and ∑ t − 1 = ∑ j =2 j / 2 − 1
j =2 j
= An 2 + Bn + C a quadratic function of n
Where A = (C5 + C 6 + C 7 ) / 4
B = C1 + C 2 + C 4 + C8 + (C 5 − C 6 − C 7 ) / 4
C = − (C 2 + C 4 + C 8 + C 5 / 2 )
Insertion Sort
• Advantages
– Good running time for “Pre-sorted” arrays “n”
• Disadvantages
– “n2” running time in worst and average case