MCS-031 - Question 1: Ans: (I) Insertion Sort
MCS-031 - Question 1: Ans: (I) Insertion Sort
First in this algorithm smallest element is being searched and kept in the first place
of the array.
Input to the program is 187,62,155,343,184,958,365,427,78,94,121,388
Small =187 //small=a[0]; here a[0] = 187
After the execution of the first for loop the values in the array look as follows:
62,187,155,343,184,958,365,427,78,94,121,388
Here number of comparisons in this for loop is 11
Step 1: i=2
62, 187, 155, 343, 184, 958, 365, 427, 78, 94, 121, 388 (for loop)
62, 155, 187, 343, 184, 958, 365, 427, 78, 94, 121, 388 (while loop)
Number of comparisons: 2
Step 2: i=3
62, 155, 187, 343, 184, 958, 365, 427, 78, 94, 121, 388 (for loop)
62, 155, 187, 343, 184, 958, 365, 427, 78, 94, 121, 388 (while loop)
Number of comparisons: 1
Step 3: i=4
62, 155, 187, 343, 184, 958, 365, 427, 78, 94, 121, 388 (for loop)
62, 155, 184, 187, 343, 958, 365, 427, 78, 94, 121, 388 (while loop)
Number of comparisons: 3
Step 4: i=5
62, 155, 184, 187, 343, 958, 365, 427, 78, 94, 121, 388 (for loop)
62, 155, 184, 187, 343, 958, 365, 427, 78, 94, 121, 388 (while loop)
Number of comparisons: 1
Step 5: i=6
62, 155, 184, 187, 343, 958, 365, 427, 78, 94, 121, 388 (for loop)
62, 155, 184, 187, 343, 365, 958, 427, 78, 94, 121, 388 (while loop)
Number of comparisons: 2
Step 6: i=7
62, 155, 184, 187, 343, 365, 958, 427, 78, 94, 121, 388 (for loop)
62, 155, 184, 187, 343, 365, 427, 958, 78, 94, 121, 388 (while loop)
Number of comparisons: 2
Step 7: i=8
62, 155, 184, 187, 343, 365, 427, 958, 78, 94, 121, 388 (for loop)
62, 78, 155, 184, 187, 343, 365, 427, 958, 94, 121, 388 (while loop)
Number of comparisons: 8
Step 8: i=9
62, 78, 155, 184, 187, 343, 365, 427, 958, 94, 121, 388 (for loop)
62, 78, 94, 155, 184, 187, 343, 365, 427, 958, 121, 388 (while loop)
Number of comparisons: 8
Step 9: i=10
62, 78, 94, 155, 184, 187, 343, 365, 427, 958, 121, 388 (for loop)
62, 78, 94, 121, 155, 184, 187, 343, 365, 427, 958, 388 (while loop)
Number of comparisons: 8
Total number of Comparisons in the above Insertion Sort algorithm are : 47.
Page 2 of 2