Bitonic Sort
Parallel sorting algorithm for power-of-2 size arrays using bitonic sequences.
Visualization
Interactive visualization for Bitonic Sort
Interactive visualization with step-by-step execution
Implementation
1function bitonicSort(arr: number[]): number[] {
2 const a = [...arr];
3 const n = a.length;
4 const cmpSwap = (i: number, j: number, dir: boolean) => {
5 if ((a[i] > a[j]) === dir) [a[i], a[j]] = [a[j], a[i]];
6 };
7 const bitonicMerge = (low: number, cnt: number, dir: boolean) => {
8 if (cnt > 1) {
9 const k = cnt >> 1;
10 for (let i = low; i < low + k; i++) cmpSwap(i, i + k, dir);
11 bitonicMerge(low, k, dir);
12 bitonicMerge(low + k, k, dir);
13 }
14 };
15 const bitonicSortRec = (low: number, cnt: number, dir: boolean) => {
16 if (cnt > 1) {
17 const k = cnt >> 1;
18 bitonicSortRec(low, k, true);
19 bitonicSortRec(low + k, k, false);
20 bitonicMerge(low, cnt, dir);
21 }
22 };
23 bitonicSortRec(0, n, true);
24 return a;
25}Deep Dive
Theoretical Foundation
Builds bitonic sequences recursively and merges them. Bitonic sequence: first monotonically increases then decreases. Optimal for parallel hardware.
Complexity
Time
O(log² n) parallel
O(n log² n) sequential
O(n log² n)
Space
O(log n)
Applications
Industry Use
GPU sorting
Hardware sorting networks
Parallel computing
Use Cases
Related Algorithms
Quicksort
A highly efficient, in-place sorting algorithm that uses divide-and-conquer strategy. Invented by Tony Hoare in 1959, it remains one of the most widely used sorting algorithms due to its excellent average-case performance and cache efficiency.
Merge Sort
A stable, divide-and-conquer sorting algorithm with guaranteed O(n log n) performance.
Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping adjacent elements if they are in the wrong order. This process is repeated until the entire array is sorted. Named for the way larger elements 'bubble' to the top (end) of the array.
Insertion Sort
Insertion Sort is a simple, intuitive sorting algorithm that builds the final sorted array one element at a time. It works similarly to how people sort playing cards in their hands - picking each card and inserting it into its correct position among the already sorted cards. Despite its O(n²) time complexity, Insertion Sort is efficient for small datasets and nearly sorted arrays, making it practical for real-world applications.