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

Three-Way Quick Sort

Partition values into less-than, equal-to, and greater-than regions so duplicate pivots finish together.

Core idea

Maintain three regions around one pivot: values below it, values equal to it, and values above it. A single scan expands the regions until no unknown values remain.

Read the visualization

The lt, i, and gt pointers bound the three colored regions. Less and greater branches swap values across a boundary, while equal values simply advance the scan.

5
3
8
3
5
8
3
5
Interval stack: each frame is a pending subarray a[lo..hi]
Stack empty: every position is final

Take the next unsorted interval from the explicit work stack.

1function quickSort3way(a: number[]): number[] {
2 const stack: [number, number][] = [[0, a.length - 1]];
3 while (stack.length > 0) {
4 const [lo, hi] = stack.pop()!;
5 const pivot = a[lo];
6 let lt = lo, i = lo, gt = hi;
7 while (i <= gt) {
8 if (a[i] < pivot) {
9 [a[lt], a[i]] = [a[i], a[lt]];
10 lt++; i++;
11 } else if (a[i] > pivot) {
12 [a[i], a[gt]] = [a[gt], a[i]];
13 gt--;
14 } else {
15 i++;
16 }
17 }
18 if (gt + 1 < hi) stack.push([gt + 1, hi]);
19 if (lt - 1 > lo) stack.push([lo, lt - 1]);
20 }
21 return a;
22}
n8
stack deep0
lo0
hi7
pivot-
lt-
i-
gt-
1 / 36

Complexity and tradeoffs

Time: Average O(n log n); worst O(n^2). Space: Average O(log n). Three-way partitioning is especially effective when many input values are equal.

Invariant: Before every comparison, positions before lt are smaller, positions from lt to i are equal, and positions after gt are greater than the pivot.

Where it fits

Three-way partitioning is the preferred Quick Sort variant for data with many duplicate keys. Distinct random data behaves similarly to ordinary Quick Sort.