Sei sulla pagina 1di 5

CS-201 Lab Manual Lab 4: Sorting: Insertion Sort

Contents:

1. Insertion Sort
2. Sample Example
3. Complexity
4. Exercises

1. Insertion Sort

Suppose an array A with n elements A[1], A[2], ……., A[N] is in memory. The insertion sort algorithm
scans A from A[1] to A[N], inserting each element A[K] into its proper position in the previously sorted
subarray A[1], A[2], ………, A[K-1]. That is:

Pass 1: A[1] by itself is trivially sorted.

Pass 2: A[2] is inserted either before or after A[1] so that: A[1], A[2] is sorted.

Pass 3: A[3] is inserted into its proper place in A[1], A[2], that is, before A[1], between A[1] and A[2],
or after A[2], so that: A[1], A[2], A[3] is sorted.

Pass 4: A[4] is inserted into its proper place in A[1], A[2], A[3] so that: A[1], A[2], A[3], A[4] is sorted.

………………………………………………………………………………………..

………………………………………………………………………………………..

Pass N: A[N] is inserted into its proper place in A[1], A[2], …….., A[N-1] so that: A[1], A[2],……, A[N]
is sorted.

This algorithm is frequently used when n is small. For example, this algorithm is very popular with bridge
players when they are first sorting their cards.

There remains only the problem of deciding how to insert A[K] in its proper place in the sorted subarray
A[1], A[2], ………., A[K-1]. This can be accomplished by comparing A[K] with A[K-1], comparing
A[K] with A[K-2], comparing A[K] with A[K-3], and so on, until first meeting an element A[J] such that
A[J] <= A[K]. Then each of the elements A[K-1], A[K-2],……., A[J+1] is moved forward one location,
and A[K] is then inserted in the J+1st position in the array.
CS-201 Lab Manual Lab 4: Sorting: Insertion Sort

2. Sample Example

Suppose an array A contains 6 elements as follows: 5, 2, 4, 6, 1, 3

Figure 4.1 illustrates the insertion sort algorithm. The colored element indicates the A[K] in each pass of
the algorithm and the black arrow indicates the proper place for inserting A[K].

Figure 4.1 – Insertion Sort for n=6 elements

The formal statement of our insertion sort algorithm follows:

INSERTION-SORT(A)
1 for j  2 to length[A]
2 do key  A[j]
3 i  j-1
4 While i > 0 and A[i] > key
5 do A[i+1]  A[i]
6 i  i-1
7 A[i+1]  key

3. Complexity

The function f(n) of comparisons in the insertion sort algorithm can be easily computed. First of all, the
worst case occurs when the array A is in reverse order and the inner loop must use the maximum number
K-1 of comparisons. Hence,

f(n) = 1+ 2+………………+ (n-1) = n(n-1)/2 = O( )


CS-201 Lab Manual Lab 4: Sorting: Insertion Sort

Furthermore, one can show that, on the average, there will be approximately (K-1)/2 comparisons in the
inner loop. Accordingly, for the average case,

f(n) = 1/2 + 2/2+ …………..+ (n-1)/2 = n(n-1)/4 = O( )

Q.1 Convert the insertion sort pseudocode in C++ code.

#include<iostream.h>
#include<conio.h>
const int size=5;
main()
{
clrscr();
int Array[size],int i,j,key;
for(i=0 ; i<size ; i++)
{
cout<<”Enter a value in the array :”;
cin>>Array[i];
}
for(i=1 ; i<size ; i++)
{
key=Array[i];
j=i-1;
while(j>-1 && Array[j]>key)
{
Array[j+1]=Array[j];
j--;
}
Array[j+1]=key;
}
for(i=0 ; i<size ; i++)
{
cout<<Array[i]<<" ";
}
getch();
}
CS-201 Lab Manual Lab 4: Sorting: Insertion Sort

Q.2 Convert the insertion sort pseudocode in C++ code

1. #include<iostream.h>
2. #include<conio.h>
3. const int size=5;
4. main()
5. {
6. clrscr();
7. int Array[size]={4,10,90,1,2};
8. int i ,j ,key;
9. for(i=1 ; i<size ; i++) //2n
10. {
11. key=Array[i]; //1
12. j=i-1; //1
13. while(j>-1 && Array[j]>key) //-1+4n/2
14. {
15. Array[j+1]=Array[j]; //1
16. j--; //1
17. }
18. Array[j+1]=key; //1
19. }
20. for(i=0 ; i<size ; i++)
21. cout<<Array[i]<<" ";
22. getch();
23. }

1 2 3 4 5 1 2 3 4 5
4 10 90 1 2

4 10 90 1 2

(a) (b)

1 2 3 4 5 1 2 3 4 5
4 10 90 1 2 1 4 10 90 2
(c )
(d)

1 2 3 4 5
(e) 1 2 4 10 90

The operation Insertion sort on the array Array= {4, 10, 90, 1, 2}.Array index appear above in the rectangle, and
values stored in array position appear within rectangles(a)-(d).The itration of the for loop of lines 9-18.In each
itration, the black rectangle holds the key taken from Array[i].which is compare with the values in shaded rectangles
CS-201 Lab Manual Lab 4: Sorting: Insertion Sort

to its left in the test of line 13.Shaded arrows show array values moved one position to the right in line 15,and black
arrows indicate where the key is moved to in line 18.(e) The final sorted array

Q.3 Calculate the time complexity for the elements of array P.


The outer for loop is execute 2n time for the element of Array= {4, 10, 90, 1, 2.}.
Inner loop execute (-1+4n/2) time for the element of Array= {4, 10, 90, 1, 2.}.
Total execution is approximately -1+6n/2 for the element of array= {4, 10, 90, 1, 2}.
Time complexity for the element of array= {4, 10, 90, 1, 2}
2n (-1+4n/2) =2n (4n-2)/2=4n2-2n=O (n2)

Potrebbero piacerti anche