From 341cf7fd8f34779beaad9a2e958fb277fccda3f8 Mon Sep 17 00:00:00 2001 From: Ashish Acharya <71539355+ashuacharya123@users.noreply.github.com> Date: Wed, 4 Oct 2023 21:11:48 +0545 Subject: [PATCH] Created shellSort.cpp --- shellSort.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 shellSort.cpp diff --git a/shellSort.cpp b/shellSort.cpp new file mode 100644 index 0000000..7d23af6 --- /dev/null +++ b/shellSort.cpp @@ -0,0 +1,33 @@ +#include +#include + +void shellSort(std::vector& arr) { + int n = arr.size(); + + for (int gap = n / 2; gap > 0; gap /= 2) { + for (int i = gap; i < n; i++) { + int temp = arr[i]; + int j; + + for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) { + arr[j] = arr[j - gap]; + } + + arr[j] = temp; + } + } +} + +int main() { + std::vector arr = {8, 3, 9, 2, 4, 7, 5, 1, 6}; + + shellSort(arr); + + std::cout << "Sorted Array: "; + for (int num : arr) { + std::cout << num << " "; + } + std::cout << std::endl; + + return 0; +}