C Program For INSERTION SORTING
Writing programs for sorting of a given set of numbers is one of the common
programming tasks. Various types of sorting techniques like selection sort,
inserting sort and quick sort are quite popular. Here C code for Insertion
sort is being presented. The program is quite simple. Here, in each iteration,
the elements are placed in their correct position. The algorithm has the complexity of O(n²)
CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[20],m=1,j,t;
clrscr();
printf("Enter the n value:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
t=a[i];
if(a[i]>a[j])
{
a[m]=a[j];
a[j]=t;
}
}
m++;
}
for(i=n;i>0;i--)
{
printf("%d\n",a[i]);
}
getch();
}

0 comments:
Post a Comment