Here is the C implementation of Insertion Sort algorithm.
/*Insertion Sort*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,k,smallest,index;
clrscr();
/*Get*/
printf("Enter the numbers to be sorted:\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
/*Sort*/
for(i=1;i<5;i++)
{
j=i;
smallest=a[i];
while(j>=0&&(smallest<a[j-1]))
{
a[j]=a[j-1];
j--;
}
a[j]=smallest;
}
/*Print*/
printf("\nThe sorted numbers are:\n");
for(i=0;i<5;i++)
{
printf("%d\n",a[i]);
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,k,smallest,index;
clrscr();
/*Get*/
printf("Enter the numbers to be sorted:\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
/*Sort*/
for(i=1;i<5;i++)
{
j=i;
smallest=a[i];
while(j>=0&&(smallest<a[j-1]))
{
a[j]=a[j-1];
j--;
}
a[j]=smallest;
}
/*Print*/
printf("\nThe sorted numbers are:\n");
for(i=0;i<5;i++)
{
printf("%d\n",a[i]);
}
getch();
}
No comments:
Post a Comment