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

Bitonic Sort

Build bitonic sequences and merge them through a fixed comparison network whose columns can run in parallel.

Core idea

A bitonic network first creates sequences that rise and then fall, then merges them with compare-exchange stages whose distances repeatedly halve. The comparator pattern depends only on positions, not values.

Read the visualization

Read the network from left to right. Comparators in one column are independent and can execute together; each highlighted pair is oriented toward the order required by its current bitonic block.

52718364

Arrange the input at the left edge of the fixed comparison network.

1function bitonicSort(a: number[]): void {
2 const n = a.length; // n as 2 init
3 for (let k = 2; k <= n; k *= 2) { // phase: init length k init
4 for (let j = k >> 1; j >= 1; j >>= 1) { // one column: compare distance j(column column column parallel)
5 for (let i = 0; i < n; i++) {
6 const l = i ^ j; // and i distance j column
7 if (l > i) {
8 const asc = (i & k) === 0; // column direction: column or column
9 if (asc ? a[i] > a[l]: a[i] < a[l])
10 [a[i], a[l]] = [a[l], a[i]]; // compare swap ( compare done)
11 }
12 }
13 }
14 }
15}
n / column count8 / 6
parallel depthlog²n level = 6 pass
1 / 8

Complexity and tradeoffs

Time: O(n log^2 n). Space: O(1) auxiliary. The data-independent network suits parallel hardware and normally expects a power-of-two length.

Invariant: After a merge stage of size k, every completed k-position block has the required monotone order.

Where it fits

Data-independent comparisons make Bitonic Sort useful on sorting networks, SIMD hardware, and GPUs. Padding or a specialized network is needed when the input length is not a power of two.