Algorithims Labmanual 4 Solve

Embed Size (px)

Citation preview

  • 8/7/2019 Algorithims Labmanual 4 Solve

    1/5

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

    Contents:

    1. Insertion Sort2. Sample Example

    3. Complexity4. Exercises

    1. Insertion Sort

    Suppose an array A with n elements A[1], A[2], ., A[N] is in memory. The insertion sort algorithmscans A from A[1] to A[N], inserting each element A[K] into its proper position in the previously sortedsubarray 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 bridgeplayers 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 subarrayA[1], A[2], ., A[K-1]. This can be accomplished by comparing A[K] with A[K-1], comparingA[K] with A[K-2], comparing A[K] with A[K-3], and so on, until first meeting an element A[J] such thatA[J]

  • 8/7/2019 Algorithims Labmanual 4 Solve

    2/5

    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-14 While i > 0 and A[i] > key5 do A[i+1] A[i]6 i i-17 A[i+1] key

    3. Complexity

    The function f (n) of comparisons in the insertion sort algorithm can be easily computed. First of all, theworst 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( )

  • 8/7/2019 Algorithims Labmanual 4 Solve

    3/5

    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 theinner 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#includeconst int size=5;main(){clrscr();int Array[size],int i,j,key;for(i=0 ; ikey)

    {Array[j+1]=Array[j];j--;

    }Array[j+1]=key;

    }for(i=0 ; i

  • 8/7/2019 Algorithims Labmanual 4 Solve

    4/5

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

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

    1. #include2. #include3. 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-1 && Array[j]>key) //-1+4n/214. {

    15. Array[j+1]=Array[j]; //116. j--; //117. }18. Array[j+1]=key; //119. }20. for(i=0 ; i

  • 8/7/2019 Algorithims Labmanual 4 Solve

    5/5

    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=4n 2-2n= O (n2)