Insertion SortIt is a very simple method to sort the number in an increasing or decreasing order. It has various advantages:
ALGORITHM: INSERTION SORT (A) 1. For k ← 2 to length [A] 2. Do key ← A[k] 3. i=k1 4. while i>0 and A[i]>key 5. do A[i+1] ← A[i] 6. i=i1 7. A[i+1] ← key Analysis:
In pass 1: no comparison is required In pass 2: 1 comparison is required In pass 3: 2 comparisons are required ............................................................................ ............................................................................... In pass n: n1 comparisons are required Total comparisons: T (n) = 1+2+3+...........+ n1 = = o (n^{2}) Therefore complexity is of order n^{2} Example:Illustrate the operation of INSERTION SORT on the array A = (4, 15, 7, 18, and 16). Solution:A [] =
For j=2 to 5 i.e.
That is
And the sorted array is:
Next TopicDivide and Conquer Introduction
