Enumeration Sort: Mechanism, Complexity Analysis & Interactive Visualizer
Enumeration Sort
A parallel ranking algorithm: determines the final sorted position of each element by counting how many elements are smaller than it, sorting values directly with zero dependency between elements.
How it works
Enumeration Sort (also known as Rank Sort) is a simple parallel algorithm. Unlike other comparison sorts that iteratively swap adjacent values (like Bubble Sort) or partition subsets (like Quicksort), Enumeration Sort finds the exact destination index of each element independently:
- Independent Rank Counting: For each element \(A[i]\) in the input list:
- Scan the entire array and count how many elements \(A[j]\) are smaller than \(A[i]\).
- To maintain stability (handling duplicate values), if \(A[j]\) is equal to \(A[i]\), increment the count only if \(j < i\).
- Direct Placement: The final rank count gives the exact index slot where \(A[i]\) belongs. Write the element directly to \(dest[\text{rank}]\).
- Perfect Parallelization: Because the rank calculations for \(A[0]\), \(A[1]\), etc. have no dependency on each other, they can all run simultaneously in parallel. On a parallel system with \(n²\) processors, the entire sort completes in \(O(1)\) time.
Complexity
Interactive visualization
Pseudocode
procedure enumerationSort(A): n ← length(A) dest ← array of size n parallel for i from 0 to n - 1: pos ← 0 for j from 0 to n - 1: if A[j] < A[i] or (A[j] == A[i] and j < i): pos ← pos + 1 dest[pos] ← A[i] copy dest back to A
JavaScript implementation
function enumerationSort(arr) { const n = arr.length; const dest = new Array(n); for (let i = 0; i < n; i++) { let pos = 0; for (let j = 0; j < n; j++) { if (arr[j] < arr[i] || (arr[j] === arr[i] && j < i)) { pos++; } } dest[pos] = arr[i]; } for (let i = 0; i < n; i++) { arr[i] = dest[i]; } return arr; }
Pros and cons
Pros
- Stunning, embarrassingly parallel architecture.
- Highly stable sorting order (using index checks).
- Zero element moves needed during the comparison phase.
Cons
- Terrible O(n²) sequential time complexity regardless of array order.
- Requires O(n) auxiliary storage.
- Impractical for single-core sequential processors.
When (not) to use it
- Massive Parallel Networks: Ideal for specialized SIMD architectures or FPGA networks where all ranks can be counted simultaneously.
- Standard Programming: Never use sequentially; comparison sorts like Mergesort or Quicksort are orders of magnitude faster.
Related reading
Sleep Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A unique concurrency-based sorting algorithm: spawns a separate thread for each element, sleeping for a duration proportional to the element value before writing it to the output.
By Md. Harun-Or-Roshid
Cube Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A parallel dimension sorting algorithm: organizes elements in a 3D coordinate cube and sorts vectors along the height, width, and depth axes sequentially to achieve sorted order.
By Md. Harun-Or-Roshid
Patience Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A card-game inspired sorting algorithm: distributes elements onto a series of piles such that each pile's top is \(\ge\) the incoming value, then performs a multi-way merge across pile tops.
By Md. Harun-Or-Roshid
Library Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A gapped variation of Insertion Sort: leaves empty slots (gaps) in the array, allowing elements to be inserted in \(O(1)\) shift time on average by avoiding massive shifts.
By Md. Harun-Or-Roshid