Insertion Sort C Program :
#include <stdio.h>
#define MAX 10
void main()
{
int a[MAX], i,n;
printf("Enter size:");
scanf("%d",&n);
generate(a,n);
printf("Random array:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
insertion_sort(a,n);
printf("\nSorted elements:\n");
display(a,n);
}
void insertion_sort(int a[],int n)
{
int temp,i,j;
for (i=0 ;i<n ;i++)
{
temp = a[i];
j = i - 1;
while (temp<a[j] && j>=0)
{
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = temp;
}
}
int display(int a[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
void generate(int a[],int n)
{
int i;
for(i=0;i<n;i++)
a[i]=rand()%100;
}
Output :
Enter size:7
Random array:
41 67 34 0 69 24 78
Sorted elements:
0 24 34 41 67 69 78
Process returned 7 (0x7) execution time : 4.474 s
Press any key to continue.
0 Comments
Thanks,To visit this blog.