You are on page 1of 1

INSERTION SORT

#include<stdio.h>
main()
{
int i,j,chk,temp_val,val[100];
printf("Please enter the total count of the elements that you want to sort: \n");
scanf("%d",&chk);
printf("Please input the elements that has to be sorted:\n");
for(i=0;i<chk;i++)
{
scanf("%d",&val[i]);
}

for(i=1;i<=chk-1;i++)
{
temp_val=val[i];
j=i-1;
while((temp_val<val[j])&&(j>=0))
{
val[j+1]=val[j];
j=j-1;
}
val[j+1]=temp_val;
}
printf("\n Output generated after using insertion sort \n");
for(i=0;i<chk;i++)
{
printf("%d ",val[i]);
}
getch();
}

You might also like