Insertion sorting technique.
■》 program to implement insertion sort in c language.
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20],N,p,i,j;
clrscr();
printf("Enter the size of array=");
scanf("%d",&N);
printf("Enter the elements=");
for(p=0; p<N; p++)
{
scanf("%d",&arr[p]);
}
printf("before sorting elements are=");
for(p=0; p<N; p++)
{
printf("%d\t",arr[p]);
}
for(i=1; i<N; i++)
{
int current=arr[i];
int j=i-1;
while(arr[j]>current && j>=0)
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=current;
}
printf("the sorted array elements are=\n");
for(p=0; p<N; p++)
{
printf("%d\t",arr[p]);
}
getch();
}
■》 Output->
Comments
Post a Comment