diff --git a/insertion_sort.c b/insertion_sort.c new file mode 100644 index 0000000000000000000000000000000000000000..42d867717853c978fe58487e2e9ebe415a834f64 --- /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