V

Algorithm Visualizer

ZHEN
Share on WeiboShare on XGitHub repositoryAuthor website
Learning ToolsAlgorithm Complexity ReferenceAlgorithm Learning Paths
Data StructuresArrayLinked ListStackQueue and DequeBinary Search TreeBinary HeapHash TableGraphTrieDisjoint Set UnionLRU CacheSkip ListSegment TreeB+ TreeBloom FilterFenwick Tree
SortingBubble SortCocktail Shaker SortBitonic SortSelection SortInsertion SortBinary Insertion SortShell SortMerge SortTop-Down Merge SortQuick SortThree-Way Quick SortDual-Pivot Quick SortHeap SortCounting SortRadix SortBucket Sort
Graph AlgorithmsDijkstra's Shortest PathKruskal's Minimum Spanning TreePrim's Minimum Spanning TreeBellman-Ford Shortest PathsTopological SortFloyd-WarshallStrongly Connected Components2-SATMaximum FlowBipartite MatchingLowest Common AncestorEulerian Path
Dynamic ProgrammingEdit Distance0/1 KnapsackUnbounded KnapsackLongest Common SubsequenceLongest Increasing SubsequenceCoin ChangeStone MergingTraveling Salesperson DPTree Dynamic ProgrammingDigit DPRerooting DP
Backtracking and SearchN-QueensSubsetsPermutationsCombination SumMaze Solving with DFSNumber of IslandsWord SearchSudoku SolverA* Search
StringsKMP String MatchingRabin-Karp String MatchingBoyer-Moore String MatchingManacher's Longest Palindromic SubstringSuffix ArrayLCP ArrayAho-Corasick AutomatonZ Function
Math and Number TheorySieve of EratosthenesLinear SieveEuclidean AlgorithmBinary ExponentiationExtended Euclidean AlgorithmChinese Remainder TheoremEuler's Totient FunctionMiller-Rabin Primality TestFast Fourier TransformPollard's Rho Factorization
Computational GeometryConvex HullRotating CalipersClosest Pair of PointsLine Segment IntersectionBentley-Ottmann Sweep Line
SearchingBinary SearchLower and Upper BoundSearch in a Rotated Sorted ArrayBinary Search on the AnswerTernary Search

Shell Sort

Run insertion sort over shrinking gaps so distant inversions disappear before the final adjacent pass.

Core idea

Apply insertion sort to elements separated by a large gap, then shrink the gap. Early passes move badly misplaced values long distances before the final gap-one cleanup.

Read the visualization

Dimmed bars are outside the current gap group. Within the active group, the key, comparisons, shifts, and insertion follow ordinary Insertion Sort at gap-sized distances.

7
6
5
10
9
8
4
3
2
1

Reduce the gap and form a new set of interleaved subsequences.

1function shellSort(a: number[]): number[] {
2 const n = a.length;
3 for (let gap = n >> 1; gap > 0; gap >>= 1) {
4 for (let start = 0; start < gap; start++) {
5 for (let i = start + gap; i < n; i += gap) {
6 const key = a[i];
7 let j = i;
8 while (j >= gap && a[j - gap] > key) {
9 a[j] = a[j - gap];
10 j -= gap;
11 }
12 a[j] = key;
13 }
14 }
15 }
16 return a;
17}
n10
gap5
group-
i0
key7
j0
a[j]7
shiftCount0
1 / 95

Complexity and tradeoffs

Time: Gap-dependent; common worst O(n^2). Space: O(1). In-place and often faster than insertion sort on medium arrays, but not stable.

Invariant: After finishing gap g, every subsequence formed by equal indices modulo g is sorted.

Where it fits

Shell Sort is compact, in-place, and practical for moderate arrays when a library sort is unavailable. Its performance depends strongly on the gap sequence and it is not stable.