0% found this document useful (0 votes)
118 views

MCS-031 - Question 1: Ans: (I) Insertion Sort

The document describes an insertion sort algorithm to sort an array of numbers. It contains the C++ code for the algorithm. The algorithm works as follows: 1) It first finds the smallest element in the array and places it in the first position. 2) It then iterates through the array, taking each element and inserting it into the sorted portion of the array by shifting elements over as needed. 3) For each pass, it calculates the number of comparisons made. For the sample input array, it performs the algorithm in 10 steps, making a total of 47 comparisons to fully sort the array.

Uploaded by

chandanspl
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

MCS-031 - Question 1: Ans: (I) Insertion Sort

The document describes an insertion sort algorithm to sort an array of numbers. It contains the C++ code for the algorithm. The algorithm works as follows: 1) It first finds the smallest element in the array and places it in the first position. 2) It then iterates through the array, taking each element and inserting it into the sorted portion of the array by shifting elements over as needed. 3) For each pass, it calculates the number of comparisons made. For the sample input array, it performs the algorithm in 10 steps, making a total of 47 comparisons to fully sort the array.

Uploaded by

chandanspl
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

MCS-031 - Question 1:

Ans: (i) Insertion Sort

/* Program to sort the numbers using Insertion sort */


#include<iostream.h>
Void main()
{
Int a[12],small,p,j,x,comp=0;
cout<<”Enter the numbers to be sorted:”;
for(int k=0;k<12;k++)
cin>>a[k];
small=a[0];
for(int i=1;i<12;i++)
{
comp++;
if(a[i]<small)
{
small=a[i];
p=I;
}
}
a[p]=a[0];
a[0]=small;
for(i=2;i<12;i++)
{
x=a[i];
j=I;
comp++;
while(x<a[j-1]
{
a[j]=a[j-1];
j=j-1;
comp++;
}
a[j]=x;
}
for(k=0;k<12;k++)
cout<<endl<<a[k];
cout<<endl<<”Comparisons are:”<<comp;
}

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

Step 10: i=11


62, 78, 94, 121, 155, 184, 187, 343, 365, 427, 958, 388 (for loop)
62, 78, 94, 121, 155, 184, 187, 343, 365, 388 427, 958, (while loop)
Number of comparisons: 3

Total number of Comparisons in the above Insertion Sort algorithm are : 47.

Page 2 of 2

You might also like