" The Techprogramiz is a computer science portal for C language. It contains well written, well thought and well explained C programming articles,basic to advance level"
Search This Blog
Program Of Insertion Sort-:
#include<stdio.h>
voidInsertionSort(inta[], intn)
{
intj, p;
inttmp;
for(p = 1; p < n; p++)
{
tmp = a[p];
for(j = p; j > 0 && a[j-1] > tmp; j--)
a[j] = a[j-1];
a[j] = tmp;
}
}
intmain()
{
inti, n, a[10];
printf("Enter the number of elements :: ");
scanf("%d",&n);
printf("Enter the elements :: ");
for(i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
InsertionSort(a,n);
printf("The sorted elements are :: ");
for(i = 0; i < n; i++)
printf("%d ",a[i]);
printf("\n");
return0;
}
Output-:Enter the number of elements :: 7
Enter the elements :: 10 30 80 20 60 40 50
The sorted elements are :: 10 20 30 40 50 60 80
follow and share
ReplyDelete