Authors:
Release Date: January 9, 2022
License: MIT License
To execute this implementation, ensure you have Python 3.x installed and follow these steps:
python ShellSort.pyShell Sort, developed by Donald Shell in 1959, is an in-place comparison sorting algorithm that serves as an optimization of Insertion Sort. It achieves higher efficiency by comparing and exchanging elements that are separated by a "gap," allowing out-of-place elements to move toward their final destinations faster than simple nearest-neighbor exchanges.
The algorithm relies on the Diminishing Increment Gap Theory.
A sequence of gaps
The complexity of Shell Sort is highly dependent on the chosen gap sequence.
-
Worst Case (Shell's Sequence):
$O(n^2)$ . -
Average Case: Generally observed between
$O(n \log^2 n)$ and$O(n^{3/2})$ for common sequences. -
Best Case:
$O(n \log n)$ for a nearly sorted array.
The space complexity remains constant:
-
$h$ -Sorted Arrays: An array is$h$ -sorted if all sub-sequences formed by taking every$h$ -th element are sorted. Shell Sort transforms the array into a series of$h$ -sorted states, where each state makes the subsequent$h$ -sorting (with a smaller$h$ ) faster. -
Improved Insertion Sort: While standard Insertion Sort is
$O(n^2)$ , it is extremely fast (linear time) on partially sorted data. Shell Sort exploits this by using large gaps to partially sort the array before finishing with a standard$h=1$ insertion sort. -
In-place Nature: Like Insertion Sort, Shell Sort modifies the array in-place, requiring no auxiliary storage scaling with
$n$ .
- Iterative Gap Reduction: Uses a
whileloop to diminish the gap until the collection is fully sorted. - Sub-sequence Processing: Employs nested loops to perform interleaved insertion sorts across the current gap distance.
- Minimal Swaps: Values are shifted rather than swapped repeatedly, reducing the number of memory write operations during each pass.
flowchart TD
A["Start: Unsorted List L"] --> B["Initialize Gap h = n/2"]
B --> C{"h > 0?"}
C -- "Yes" --> D["Iterate i from h to n-1"]
D --> E["Temp = L[i]"]
E --> F["Shift Elements with Δh until Temp Position Found"]
F --> G["L[j] = Temp"]
G --> H["Remaining Elements in h-Subsequence?"]
H -- "Yes" --> D
H -- "No" --> I["Diminish Gap: h = h // 2"]
I --> C
C -- "No" --> J["Stop: List h-Sorted for h=1"]
graph LR
subgraph GapTheory ["Gap Sequence Progression"]
direction LR
g1["h = n/2"] --> g2["h = n/4"] --> g3["..."] --> g4["h = 1"]
end
