diff --git a/insertion_sort b/insertion_sort new file mode 100755 index 0000000000000000000000000000000000000000..451612e61d0369ed50467468f2a51404178f19f4 Binary files /dev/null and b/insertion_sort differ diff --git a/insertion_sort.c b/insertion_sort.c new file mode 100644 index 0000000000000000000000000000000000000000..c9049929843f6cd85b5b0ddc56baec30a91405b8 --- /dev/null +++ b/insertion_sort.c @@ -0,0 +1,41 @@ +/* Copyrights http://www.programmingsimplified.com/c/source-code/c-program-insertion-sort */ +/* insertion sort ascending order */ + +#include <stdio.h> +#define MaxElmt 10000 + +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++) { + array[c] = rand() % MaxElmt; + } + + 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; +} + +