Insertion Sort Code

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

#include<stdio.

h>
#include<conio.h>
#define SIZE 100
int main()
{
int i,j,t,n;
int a[SIZE];
printf("How Many Numbers User Wants to sort : ");
scanf("%d",&n);
printf("Enter %d Numbers to Sort :\n ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Before Sort : ");
for(i=0;i<n;i++)
{
printf("% d",a[i]);
}
//Sort
for(i=0;i<n;i++)
{
t=a[i];
j=i-1;
while(j>=0 && t<a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1]=t;
}
//Ascending
printf("\nOrder Using Insertion Sort : ");
for(i=0;i<n;i++)
{
printf("% d",a[i]);

}
return 0;
getch();
}

You might also like