/*write a program to implement insertion sort*/
#include<stdio.h>
void insertion_sort(int arr[],int c)
{
printf("****INSERTION
SORT****\n\n");
int i,j,temp;
printf("\n\n\tThe
unsorted array is: ");
for(i=0;i<c;i++)
{
printf("%d ,",arr[i]);
}
for(i=1;i<c;i++)
{
temp=arr[i];
j=i-1;
while((temp<arr[j])&&(j>=0))
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
}
printf("\n\n\tSorted
array is:\n");
for(i=0;i<c;i++)
{
printf("%d ,",arr[i]);
}
}
int main()
{
int t[50],k,limit,i;
printf("how many
element you want to enter: ");
scanf("%d",&limit);
printf("enter the
array: ");
for(k=0;k<limit;k++)
scanf("%d",&t[k]);
insertion_sort(t,k);
}
return 0;
}
No comments:
Post a Comment