Write a ‘C’ program to sort an array elements using Insert Sort technique.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],t,n,i,j,k;
clrscr();
printf("\n enter how many number=");
scanf(" %d",&n);
printf("\n Enter %d numbres:-",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Before sorting numbers are:-");
for(i=0;i<n;i++)
{
printf("\n %d",a[i]);
}
for(j=0;j<n;j++)
{
k=a[j];
if(j==0)
{
a[j]=k;
}
else
{
for(t=j-1;t>=0&&k<a[t];t--)
{
a[t+1]=a[t];
}
a[t+1]=k;
}
}
printf("\n after sorting numbers are=\n");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
getch();
}
/*
enter how many number=5
Enter 5 numbres:-5
1
6
0
-8
Before sorting numbers are:-
5
1
6
0
-8
after sorting numbers are=
-8
0
1
5
6 */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],t,n,i,j,k;
clrscr();
printf("\n enter how many number=");
scanf(" %d",&n);
printf("\n Enter %d numbres:-",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Before sorting numbers are:-");
for(i=0;i<n;i++)
{
printf("\n %d",a[i]);
}
for(j=0;j<n;j++)
{
k=a[j];
if(j==0)
{
a[j]=k;
}
else
{
for(t=j-1;t>=0&&k<a[t];t--)
{
a[t+1]=a[t];
}
a[t+1]=k;
}
}
printf("\n after sorting numbers are=\n");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
getch();
}
/*
enter how many number=5
Enter 5 numbres:-5
1
6
0
-8
Before sorting numbers are:-
5
1
6
0
-8
after sorting numbers are=
-8
0
1
5
6 */