Shell Sort
Generalized insertion sort using gap sequences. Invented by Donald Shell in 1959. Sorts elements at specific intervals (gaps), gradually reducing gap to 1. More efficient than insertion sort for larger arrays.
Visualization
Interactive visualization for Shell Sort
Shell Sort Visualization
Interactive visualization with step-by-step execution
Implementation
1function shellSort(arr: number[]): number[] {
2 const sorted = [...arr];
3 const n = sorted.length;
4
5 // Start with large gap, reduce by half
6 for (let gap = Math.floor(n / 2); gap > 0; gap = Math.floor(gap / 2)) {
7 // Gapped insertion sort
8 for (let i = gap; i < n; i++) {
9 const temp = sorted[i];
10 let j = i;
11
12 while (j >= gap && sorted[j - gap] > temp) {
13 sorted[j] = sorted[j - gap];
14 j -= gap;
15 }
16
17 sorted[j] = temp;
18 }
19 }
20
21 return sorted;
22}Deep Dive
Theoretical Foundation
Uses diminishing increment (gap) to sort. Starts with large gap, reduces to 1. Each pass sorts elements gap positions apart. Common gap sequences: Shell's (n/2, n/4, ...), Knuth's (3^k-1), Sedgewick's. Performance depends on gap sequence choice.
Complexity
Time
O(n log n)
O(n log² n)
O(n²)
Space
O(1)
Applications
Use Cases
Related Algorithms
Quicksort
A highly efficient, in-place sorting algorithm that uses divide-and-conquer strategy. Invented by Tony Hoare in 1959, it remains one of the most widely used sorting algorithms due to its excellent average-case performance and cache efficiency.
Merge Sort
A stable, divide-and-conquer sorting algorithm with guaranteed O(n log n) performance.
Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping adjacent elements if they are in the wrong order. This process is repeated until the entire array is sorted. Named for the way larger elements 'bubble' to the top (end) of the array.
Insertion Sort
Insertion Sort is a simple, intuitive sorting algorithm that builds the final sorted array one element at a time. It works similarly to how people sort playing cards in their hands - picking each card and inserting it into its correct position among the already sorted cards. Despite its O(n²) time complexity, Insertion Sort is efficient for small datasets and nearly sorted arrays, making it practical for real-world applications.