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

Quick Sort

Divide and conquer sorting with visible partition boundaries

Put one pivot in its final place

Quick Sort chooses a pivot, partitions the current range so smaller values are on its left and larger values are on its right, then repeats the same job on both sides. Once partitioning finishes, that pivot is already in its final sorted position.

What the visualization shows

This page uses Lomuto partitioning with the last value as the pivot. The side panel is an explicit interval stack that replaces recursion. Watch the scan pointer move, the less-than region grow, and each pivot become fixed. Change the input to test favorable, reversed, or duplicate-heavy data.

7
6
5
10
9
8
4
3
2
1
Interval stack: each frame is a pending subarray a[lo..hi]
Stack empty: every position is final

Pop interval [0, 9] from the work stack.

1function quickSort(a: number[]): number[] {
2 const n = a.length;
3 const stack: [number, number][] = [[0, n - 1]];
4 while (stack.length > 0) {
5 const [lo, hi] = stack.pop()!;
6 if (lo >= hi) continue;
7 const pivot = a[hi];
8 let i = lo;
9 for (let j = lo; j < hi; j++) {
10 if (a[j] < pivot) {
11 [a[i], a[j]] = [a[j], a[i]];
12 i++;
13 }
14 }
15 [a[i], a[hi]] = [a[hi], a[i]];
16 stack.push([i + 1, hi]);
17 stack.push([lo, i - 1]);
18 }
19 return a;
20}
n10
Stack depth0
lo0
hi9
pivot-
i-
j-
a[j]-
swapCount0
1 / 93

Complexity and tradeoffs

Average time is O(n log n), auxiliary stack space is typically O(log n), and partitioning is in-place. Quick Sort is not stable. Consistently bad pivots can produce O(n^2) time, so practical implementations randomize or improve pivot selection.

Invariant: before each scan step, values before i are smaller than the pivot, while values from i through the previous scan position are at least the pivot. Keeping that statement true explains why the final pivot swap is correct.

Compare its growth rate in the complexity reference, or continue with Binary Search to see another logarithmic divide-and-conquer pattern.