Insertion Sort in C : In insertion sort the element is inserted at an appropriate place similar to card insertion. Here the list is divided into two parts sorted and unsorted sub-lists. In each pass, the first element of unsorted sub list is picked up and moved into the sorted sub list by inserting it in suitable position.Insertion sort works this way:

## Insertion Sort in C

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #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 of Insertion Short in C**

You can learn about Binary Search Tree program for Insertion, Deletion, and Traversal in C here and for more quires go to quora

## 2 thoughts on “C Program for Insertion Sort”

Comments are closed.