#include<stdio.h>
void insertion_sort(int *arr,int n)
{
int i,j,tmp;
for(i=1;i<n;i++)
{
tmp=arr[i];
for(j=i-1;j>=0&&arr[j]>tmp;arr[j+1]=arr[j],j--);
arr[j+1]=tmp;
}
}
void display(int *arr,int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
}
int main()
{
int *arr,n,i;
printf("Enter the no. of elements : ");
scanf("%d",&n);
arr=(int*)malloc(sizeof(int)*n);
printf("\n\nEnter %d elements :",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("\n\nBefore Sorting :");
display(arr,n);
insertion_sort(arr,n);
printf("\n\nAfter Sorting :");
display(arr,n);
return 0;
}
No comments:
Post a Comment