From 51486e902585faa2b3683901504fb6417ccfa3e3 Mon Sep 17 00:00:00 2001 From: Ahmad Shahab <ashahab28@gmail.com> Date: Fri, 12 Feb 2016 09:33:33 +0700 Subject: [PATCH] Sorting algorithm reference --- insertion_sort.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 insertion_sort.c diff --git a/insertion_sort.c b/insertion_sort.c new file mode 100644 index 0000000..42d8677 --- /dev/null +++ b/insertion_sort.c @@ -0,0 +1,38 @@ +/* Copyrights http://www.programmingsimplified.com/c/source-code/c-program-insertion-sort */ +/* insertion sort ascending order */ + +#include <stdio.h> + +int main() +{ + int n, array[1000], c, d, t; + + 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++) { + d = c; + + while ( d > 0 && array[d] < array[d-1]) { + 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]); + } + + return 0; +} \ No newline at end of file -- GitLab