Insertion Sort
an example of how the algorithm works on a list of 5 integers:
code for Insertion Sort:-
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,n,m,*arr,temp,temp1,loc=0,flag;
printf("enter the amount of number to be sort\n");
scanf("%d",&n);
printf("enter the values to be sort\n");
arr=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(i=1;i<n;i++)
{
flag=0;
temp=arr[i];
for(j=i-1;j>=0;j--)
{
if(temp<arr[j])
{
arr[j+1]=arr[j];
loc=j;
flag++;
}
}
if(flag>0)
arr[loc]=temp;
}
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
system("\n pause");
return 0;
}
No comments:
Post a Comment