1 Insertion Sort PDF

You might also like

You are on page 1of 1

Assignment No: 1 Date:17/06/19

Implement Insertion Sort

#include<stdio.h>
#include<conio.h>
int main()
{
int i,t,a[100],d,n,c=0;
printf("Enter the no of elements: ");
scanf("%d",&n);
printf("Enter %d Integers: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n-1;i++)
{
d=i;
while(d>0&&a[d]<a[d-1])
{
c++;
t=a[d];
a[d]=a[d-1];
a[d-1]=t;
d--;
}
}
printf("Sorted List in Ascending Order is:\n");
for(i=0;i<=n-1;i++)
{
printf("\t%d",a[i]);
}
printf("\nTotal number of comparison= %d",c);
getch();

You might also like