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

Bucket Sort

Scatter values across numeric ranges, sort each bucket independently, and concatenate the ranges in order.

Core idea

Map each value into an ordered numeric range, sort values within each range, then concatenate the buckets. Distribution turns one global problem into several smaller local ones.

Read the visualization

The range label below each bucket explains every distribution choice. Buckets first contain arrival order, then show their local sort, and finally drain into the output array.

29
25
3
49
9
37
21
43
0–9
10–19
29
20–29
30–39
40–49

Place the highlighted value into the bucket covering its numeric range.

1function bucketSort(a: number[]): number[] {
2 const buckets: number[][] = Array.from({ length: 5 }, () => []);
3 for (const x of a) buckets[Math.min((x / 10) | 0, 4)].push(x);
4 for (const b of buckets) b.sort((x, y) => x - y);
5 let w = 0;
6 for (const b of buckets)
7 for (const x of b) a[w++] = x;
8 return a;
9}
n8
phasedistribute
i0
v29
bucket20–29
1 / 22

Complexity and tradeoffs

Time: Average O(n+k); worst O(n^2). Space: O(n+k). Near-linear behavior assumes values distribute reasonably evenly across k buckets.

Invariant: Every value in bucket i is no greater than every value in a later nonempty bucket, so concatenating sorted buckets is globally sorted.

Where it fits

Bucket Sort approaches linear time for known, roughly uniform distributions. Skewed data can overload one bucket and recover the cost of its inner sorting method.