1- /* *
2- * Shell_Sort.cpp
3- *
4- * Shell Sort Algorithm
5- *
6- * Description:
7- * Shell Sort is an in-place comparison sort which generalizes insertion sort
8- * by allowing exchanges of elements that are far apart. The idea is to
9- * arrange the list of elements so that, starting anywhere, taking every h-th
10- * element (for some gap h) yields a sorted list. Such a list is said to be
11- * h-sorted. By h-sorting the list for a decreasing sequence of gaps (ending
12- * with 1), the array gets closer to fully sorted and the final insertion sort
13- * (gap = 1) is fast because the array is already mostly sorted.
14- *
15- * Approach / Methodology:
16- * - Use a sequence of gaps (here the simple sequence gap = n/2, gap /= 2).
17- * - For each gap, perform a "gapped insertion sort": treat each subarray formed
18- * by elements at indices i, i+gap, i+2*gap, ... as an insertion-sorted list.
19- * - Reduce the gap until it becomes 1 (a normal insertion sort finishes the job).
20- *
21- * Use cases:
22- * - Good for medium-sized arrays.
23- * - Often used as an educational example of diminishing increment sorting.
24- * - Faster than plain insertion sort for arrays that are not too large and/or
25- * partially sorted.
26- *
27- * Complexity Analysis:
28- * - Time complexity:
29- * * Depends strongly on the gap sequence.
30- * * With the simple gap sequence (gap = n/2, gap /= 2) the worst-case
31- * time complexity can be O(n^2).
32- * * With better gap sequences (e.g., Knuth, Tokuda) practical performance
33- * is considerably improved.
34- * - Space complexity: O(1) (in-place sorting, only a few extra variables).
35- *
36- * Notes:
37- * - This implementation uses the simple gap sequence (n/2, n/4, ..., 1).
38- * - For production or performance-sensitive code, consider using a better gap
39- * sequence (Knuth's sequence: gap = gap*3 + 1).
40- *
41- * Author: Shubham Khetan (https://github.com/Shubham-Khetan-2005)
42- */
43-
441#include < bits/stdc++.h>
452using namespace std ;
463
47- /* *
48- * shellSort
49- * -----------
50- * Sorts the vector `arr` in ascending order using Shell Sort.
51- *
52- * Parameters:
53- * - arr : reference to a vector<int> that will be sorted in-place.
54- *
55- * Returns:
56- * - void (the input vector is modified directly)
57- */
58- void shellSort (vector<int > &arr) {
59- int n = static_cast <int >(arr.size ());
60-
61- // Start with a big gap, then reduce the gap
62- // Here gap sequence: n/2, n/4, ..., 1 (Shell's original sequence)
4+ void shellSort (vector<int >& arr) {
5+ int n = arr.size ();
636 for (int gap = n / 2 ; gap > 0 ; gap /= 2 ) {
64-
65- // Perform a gapped insertion sort for this gap size.
66- // The elements arr[i], arr[i-gap], arr[i-2*gap], ... are compared and shifted.
677 for (int i = gap; i < n; ++i) {
68- int temp = arr[i]; // Element to be inserted in the sorted subarray
8+ int temp = arr[i];
699 int j = i;
70-
71- // Shift earlier gap-sorted elements up until the correct location for temp is found
72- // We compare arr[j - gap] with temp and shift arr[j - gap] to arr[j] if it's greater.
7310 while (j >= gap && arr[j - gap] > temp) {
7411 arr[j] = arr[j - gap];
7512 j -= gap;
7613 }
77-
78- // Place temp (the original arr[i]) in its correct location
7914 arr[j] = temp;
8015 }
8116 }
8217}
8318
84- /* Utility function to print a vector nicely */
85- void printVector (const vector<int > &v) {
86- for (size_t i = 0 ; i < v.size (); ++i) {
87- if (i) cout << ' ' ;
88- cout << v[i];
89- }
90- cout << ' \n ' ;
91- }
92-
93- /* Test harness that demonstrates the algorithm with multiple test cases */
94- void run_tests () {
95- cout << " Shell Sort - Demo Test Cases\n\n " ;
96-
97- // Test case 1: random numbers
98- vector<int > t1 = {12 , 34 , 54 , 2 , 3 , 7 };
99- cout << " Test 1 (random):\n Before: " ;
100- printVector (t1);
101- shellSort (t1);
102- cout << " After : " ;
103- printVector (t1);
104- cout << " Expected: 2 3 7 12 34 54\n\n " ;
105-
106- // Test case 2: already sorted (best case for insertion-like sorts)
107- vector<int > t2 = {1 , 2 , 3 , 4 , 5 , 6 };
108- cout << " Test 2 (already sorted):\n Before: " ;
109- printVector (t2);
110- shellSort (t2);
111- cout << " After : " ;
112- printVector (t2);
113- cout << " Expected: 1 2 3 4 5 6\n\n " ;
114-
115- // Test case 3: reverse sorted (worst-ish case)
116- vector<int > t3 = {9 , 8 , 7 , 6 , 5 , 4 , 3 };
117- cout << " Test 3 (reverse sorted):\n Before: " ;
118- printVector (t3);
119- shellSort (t3);
120- cout << " After : " ;
121- printVector (t3);
122- cout << " Expected: 3 4 5 6 7 8 9\n\n " ;
123-
124- // Test case 4: contains duplicates
125- vector<int > t4 = {5 , 3 , 8 , 5 , 2 , 9 , 5 };
126- cout << " Test 4 (duplicates):\n Before: " ;
127- printVector (t4);
128- shellSort (t4);
129- cout << " After : " ;
130- printVector (t4);
131- cout << " Expected: 2 3 5 5 5 8 9\n\n " ;
132-
133- // Test case 5: edge cases - empty and single-element arrays
134- vector<int > t5 = {};
135- vector<int > t6 = {42 };
136- cout << " Test 5 (empty):\n Before: " ;
137- printVector (t5);
138- shellSort (t5);
139- cout << " After : " ;
140- printVector (t5);
141- cout << " Expected: (empty line)\n\n " ;
142-
143- cout << " Test 6 (single element):\n Before: " ;
144- printVector (t6);
145- shellSort (t6);
146- cout << " After : " ;
147- printVector (t6);
148- cout << " Expected: 42\n\n " ;
149-
150- cout << " All demo tests finished.\n " ;
151- }
152-
15319int main () {
15420 ios::sync_with_stdio (false );
15521 cin.tie (nullptr );
15622
157- // NOTE:
158- // This file includes a demonstration test harness (`run_tests()`).
159- // If you want to use this program to sort custom input, comment out run_tests()
160- // and uncomment the interactive input block below.
23+ vector<int > a = {170 , 45 , 75 , 90 , 802 , 24 , 2 , 66 };
16124
162- run_tests ( );
25+ shellSort (a );
16326
164- /* Interactive / competitive programming style input:
165- * Uncomment this block to enable stdin-based input:
166- *
167- * Example input:
168- * 6
169- * 12 34 54 2 3 7
170- *
171- * Expected output:
172- * 2 3 7 12 34 54
173- */
174-
175- /*
176- int n;
177- if (!(cin >> n)) return 0; // read n; exit if no input provided
178- vector<int> arr(n);
179- for (int i = 0; i < n; ++i) cin >> arr[i];
180-
181- shellSort(arr);
182-
183- for (int x : arr) cout << x << ' ';
27+ for (int x : a) cout << x << ' ' ;
18428 cout << ' \n ' ;
185- */
186-
18729 return 0 ;
188- }
30+ }
0 commit comments