Bead Sort (Gravity Sort)
Physical sorting algorithm simulating gravity on beads, O(n) or O(√n) depending on implementation.
Visualization
Interactive visualization for Bead Sort (Gravity Sort)
Interactive visualization with step-by-step execution
Implementation
1function beadSort(arr: number[]): number[] {
2 if (!arr.length) return [];
3 const max = Math.max(...arr);
4 const beads = Array.from({length: arr.length}, () => Array(max).fill(0));
5 for (let i = 0; i < arr.length; i++)
6 for (let j = 0; j < arr[i]; j++) beads[i][j] = 1;
7 const result: number[] = [];
8 for (let j = 0; j < max; j++) {
9 let sum = 0;
10 for (let i = 0; i < arr.length; i++) sum += beads[i][j];
11 for (let i = arr.length - sum; i < arr.length; i++) beads[i][j] = 1;
12 }
13 for (let i = 0; i < arr.length; i++) {
14 let count = 0;
15 for (let j = 0; j < max && beads[i][j]; j++) count++;
16 result[i] = count;
17 }
18 return result.reverse();
19}Deep Dive
Theoretical Foundation
Models gravity acting on beads on vertical rods. Each number represented as beads; gravity pulls them down simultaneously, resulting in sorted order.
Complexity
Time
O(n*m)
O(n*m)
O(n*m)
Space
O(n*m)
Applications
Industry Use
Educational visualization
Theoretical CS
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.