Ans:->
The program to implement insertion sort in C Language.
Program is as follows:--
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t, flag = 0;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 1 ; c <= n - 1; c++)
{
t = array[c];
for (d = c - 1 ; d >= 0; d--)
{
if (array[d] > t)
{
array[d+1] = array[d];
flag = 1;
}
else
break;
}
if (flag)
array[d+1] = t;
}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++)
{
printf("%d\n", array[c]);
}
return 0;
}
OUTPUT:----
Enter number of elements
5
Enter 5 integers
86
52
2
35
16
Sorted list in ascending order:
2
16
35
52
86
0 Comments
If you want to give any suggestion regarding the post please feel free to Comment below-