Tweet
/* Insertion sort */
#include<stdio.h>
int swap(int *a,int x,int y)
{
int temp=*(a+x);
*(a+x)=*(a+y);
*(a+y)=temp;
}
void insertion_sort(int a[],int n)
{
int temp,i,j;
for(i=1;i<n;i++)
{
temp=a[i];
for(j=i-1;((j>=0)&&(temp<a[j]));j--)
{
a[j+1]=a[j];
}
a[j+1]=temp;
}
printf("SORTED ARRAY : ");
for(i=0;i<n;i++)
printf("%4d",a[i]);
printf("\n");
}
main()
{
int i,n;
printf("Enter array capacity : \n");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
*(a+i)=rand()%100;
printf("INITIAL ARRAY : ");
for(i=0;i<n;i++)
printf("%4d",a[i]);
printf("\n");
insertion_sort(a,n);
}
Insertion Sort
Posted by
LAHAUL SETH
~
Insertion Sort
2011-11-29T10:39:00+05:30
LAHAUL SETH
Programming in C
|
Sorting
|
Comments
Insertion Sort
2011-11-29T10:39:00+05:30
LAHAUL SETH
Programming in C
|
Sorting
|