From 25e071bff4f2d3b639c2b376b1c267a4b2055afe Mon Sep 17 00:00:00 2001 From: Night101 <91742459+notaweed@users.noreply.github.com> Date: Sat, 2 Oct 2021 11:54:52 +0530 Subject: [PATCH] Create insertionSort.c --- insertionSort.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 insertionSort.c diff --git a/insertionSort.c b/insertionSort.c new file mode 100644 index 0000000..7fda55a --- /dev/null +++ b/insertionSort.c @@ -0,0 +1,36 @@ +#include + +void printArray(int* A, int n){ + for (int i = 0; i < n; i++) + { + printf("%d ", A[i]); + } + printf("\n"); +} + +void insertionSort(int *A, int n){ + int key, j; + // Loop for passes + for (int i = 1; i <= n-1; i++) + { + key = A[i]; + j = i-1; + // Loop for each pass + while(j>=0 && A[j] > key){ + A[j+1] = A[j]; + j--; + } + A[j+1] = key; + } +} + +int main() { + int A[] = {12, 54, 65, 7, 23, 9}; + int n = 6; + printf("Before Sorting: "); + printArray(A, n); + insertionSort(A, n); + printf("After Sorting: "); + printArray(A, n); + return 0; +}