Pigeonhole Sort
Distribution sort similar to counting sort for small integer ranges.
Visualization
Interactive visualization for Pigeonhole Sort
Pigeonhole Sort Visualization
Interactive visualization with step-by-step execution
Implementation
1function pigeonholeSort(arr: number[]): number[] {
2 if (!arr.length) return [];
3 const min = Math.min(...arr), max = Math.max(...arr);
4 const range = max - min + 1;
5 const holes: number[][] = Array.from({length: range}, () => []);
6 for (const val of arr) holes[val - min].push(val);
7 const result: number[] = [];
8 for (const hole of holes) result.push(...hole);
9 return result;
10}Deep Dive
Theoretical Foundation
Creates pigeonholes (buckets) for each possible value in range, distributes items, then collects in order.
Complexity
Time
O(n + range)
O(n + range)
O(n + range)
Space
O(n + range)
Applications
Industry Use
Small integer ranges
Grade sorting
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.