//Insertion Sort in C
#include<stdio.h>
#include<conio.h>
void
main()
{
int a[100],n,temp,i,j;
clrscr();
printf("Sorting using insertion sort
technique\n");
//Enter how many
elements
printf("How many elements u want to
sort\n");
scanf("%d",&n);
//entry of
elements
printf("Enter elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
temp
= a[i];
j=i-1;
while(temp<a[j]
&& j>=0)
{
a[j+1]
= a[j];
--j;
}
a[j+1]=temp;
}
printf("In
ascending order: ");
for(i=0;
i<n; i++)
printf("%d\t",a[i]);
getch();
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.