Monday, May 5, 2014

Insertion Sort in C

================================================================
*INSERTION SORT*/
=============================================================
#include<stdio.h>

void main()
{
int arr[100],temp,step,comp,s;
printf("Enter the size of array:=");
scanf("%d",&s);
printf("Now enter %d numbers in array arr[%d]=>>\n",s,s);

for(int i=0;i<s;i++)
{
scanf("%d",&arr[i]);
}

for(step=1;step<s;step++)
{
comp=step;
while(comp>0 && arr[comp]<arr[comp-1])
{
temp=arr[comp];
arr[comp]=arr[comp-1];
arr[comp-1]=temp;
comp--;
}
}

 printf("Now After Insertion sort my array is=>>\n");
 for(i=0;i<s;i++)
{
printf("%d\n",arr[i]);
}
}

No comments:

Post a Comment