From e1903d953bcca7d08945e17e740213fcbab0c464 Mon Sep 17 00:00:00 2001 From: PRIYANSH <83279155+Priyansh-777@users.noreply.github.com> Date: Wed, 12 Oct 2022 19:46:33 +0530 Subject: [PATCH 1/3] C++ Implementation of Insertion Sort --- Insertion Sort | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Insertion Sort diff --git a/Insertion Sort b/Insertion Sort new file mode 100644 index 0000000..4669ecf --- /dev/null +++ b/Insertion Sort @@ -0,0 +1,42 @@ +// C++ program for insertion sort + +#include + +void insertionSort(int arr[], int n) +{ + int i, key, j; + for (i = 1; i < n; i++) + { + key = arr[i]; + j = i - 1; + + + while (j >= 0 && arr[j] > key) + { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } +} + +void printArray(int arr[], int n) +{ + int i; + for (i = 0; i < n; i++) + cout << arr[i] << " "; + cout << endl; +} + + +int main() +{ + int arr[] = { 12, 11, 13, 5, 6 }; + int N = sizeof(arr) / sizeof(arr[0]); + + insertionSort(arr, N); + printArray(arr, N); + + return 0; +} + From c3bd0c7abd9a0487d678896cf220073c223cd858 Mon Sep 17 00:00:00 2001 From: PRIYANSH <83279155+Priyansh-777@users.noreply.github.com> Date: Wed, 12 Oct 2022 19:47:27 +0530 Subject: [PATCH 2/3] Delete Insertion Sort --- Insertion Sort | 42 ------------------------------------------ 1 file changed, 42 deletions(-) delete mode 100644 Insertion Sort diff --git a/Insertion Sort b/Insertion Sort deleted file mode 100644 index 4669ecf..0000000 --- a/Insertion Sort +++ /dev/null @@ -1,42 +0,0 @@ -// C++ program for insertion sort - -#include - -void insertionSort(int arr[], int n) -{ - int i, key, j; - for (i = 1; i < n; i++) - { - key = arr[i]; - j = i - 1; - - - while (j >= 0 && arr[j] > key) - { - arr[j + 1] = arr[j]; - j = j - 1; - } - arr[j + 1] = key; - } -} - -void printArray(int arr[], int n) -{ - int i; - for (i = 0; i < n; i++) - cout << arr[i] << " "; - cout << endl; -} - - -int main() -{ - int arr[] = { 12, 11, 13, 5, 6 }; - int N = sizeof(arr) / sizeof(arr[0]); - - insertionSort(arr, N); - printArray(arr, N); - - return 0; -} - From c0ba5d214629479f8452f9ca1ee419b4491f3903 Mon Sep 17 00:00:00 2001 From: PRIYANSH <83279155+Priyansh-777@users.noreply.github.com> Date: Wed, 12 Oct 2022 19:48:15 +0530 Subject: [PATCH 3/3] C++ Implementation of Insertion Sort --- Arrays/Insertion Sort | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Arrays/Insertion Sort diff --git a/Arrays/Insertion Sort b/Arrays/Insertion Sort new file mode 100644 index 0000000..5745206 --- /dev/null +++ b/Arrays/Insertion Sort @@ -0,0 +1,42 @@ +// C++ program for insertion sort + +#include +using namespace std; + + +void insertionSort(int arr[], int n) +{ + int i, key, j; + for (i = 1; i < n; i++) + { + key = arr[i]; + j = i - 1; + + while (j >= 0 && arr[j] > key) + { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } +} + +void printArray(int arr[], int n) +{ + int i; + for (i = 0; i < n; i++) + cout << arr[i] << " "; + cout << endl; +} + + +int main() +{ + int arr[] = { 12, 11, 13, 5, 6 }; + int N = sizeof(arr) / sizeof(arr[0]); + + insertionSort(arr, N); + printArray(arr, N); + + return 0; +}