Insertion Sorting Method – C Program
write a c program to accept array from user and sorting all element using Insertion sort method.
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20],i,j,k,temp, no;
printf("Enter size of array=\n");
scanf("%d",&no);
printf("\nPlease enter Elements of Array =\n");
for(i=0; i<no; i++)
{
scanf("%d",&arr[i]);
}
for(i=0; i<no; i++)
{
temp=a[i];
for(j=i-1; j>=0 && temp<a[j]; j--)
{
arr[j+1]=arr[j];
}
arr[j+1]=temp;
}
printf("The Sorted Array is = \n");
for(i=0;i<no;i++)
{
printf("%d\t",arr[i]);
}
getch();
}
The output of Insertion sort c program is :
Enter size of array =
7
Please enter Elements of Array =
8
25
84
23
56
9
36
The Sorted Array is =
8 9 23 25 36 56 84