From 1327a9403aaa5e8fc556e35d80704da1f16be135 Mon Sep 17 00:00:00 2001 From: Preetraj Haldar Date: Fri, 21 Oct 2022 21:34:46 +0530 Subject: [PATCH] Create insertionSort.cpp --- insertionSort.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 insertionSort.cpp diff --git a/insertionSort.cpp b/insertionSort.cpp new file mode 100644 index 0000000..0459fe3 --- /dev/null +++ b/insertionSort.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; +int insertion(int *a, int n) +{ + for (int i = 0; i < n; i++) + { + int temp = a[i]; + int j = i - 1; // less than 1 + + while (j >= 0 && a[j] > temp) + { + a[j + 1] = a[j]; + j--; + } + a[j + 1] = temp; + } + return 0; +} + +void print(int *a, int n) +{ + for (int i = 0; i < n; i++) + { + cout << a[i] << ","; + } +} +int main() +{ + int n=9; + int a[] = {12, 13, 8, 2, 16, 5, 0, 1, 4}; + + insertion(a, n); + print(a, n); +}