From c4fe565a941c734dfeecc45d6d3cff2eee4f31a8 Mon Sep 17 00:00:00 2001 From: JavvajiSumanth99 <56475971+JavvajiSumanth99@users.noreply.github.com> Date: Sat, 12 Oct 2019 17:27:36 +0530 Subject: [PATCH] C Program for Insertion Sort A C Program designed to sort the elements using Insertion Sort. --- Insertion Sort | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Insertion Sort diff --git a/Insertion Sort b/Insertion Sort new file mode 100644 index 0000000..5801d64 --- /dev/null +++ b/Insertion Sort @@ -0,0 +1,34 @@ +#include + +int main() +{ + int n, array[1000], c, d, t; // declaration of variables + + printf("Enter number of elements\n"); + scanf("%d", &n); // size of the array + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 1 ; c <= n - 1; c++) { + d = c; + + while ( d > 0 && array[d-1] > array[d]) { + t = array[d]; + array[d] = array[d-1]; + array[d-1] = t; + + d--; + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c <= n - 1; c++) { + printf("%d\n", array[c]); // print the sorted elements + } + + return 0; +}