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; +}