Slow Sort: Mechanism, Complexity Analysis & Interactive Visualizer
Slow Sort
A humorous sorting algorithm based on the "multiply and surrender" paradigm: recursively sorts halves, swaps the mid and end values to find the local max, and recurse again on the remaining prefix.
How it works
Slow Sort (or Slowsort) was published in 1984 by Andrei Broder and Jorge Stolfi. It is a joke algorithm designed to be as slow as possible while still eventually sorting correctly. Its logic operates on the "multiply and surrender" design pattern:
- To sort the sub-segment from index \(i\) to \(j\):
- **Left Half Recurse:** Sort the left half: `slowsort(A, i, mid)`.
- **Right Half Recurse:** Sort the right half: `slowsort(A, mid + 1, j)`.
- **Local Max Swap:** Compare the last elements of both halves: \(A[mid]\) and \(A[j]\). If \(A[mid] > A[j]\), swap them. This comparison guarantees that the overall maximum element of the range is placed at the end index \(j\).
- **Surrender Recurse:** Now that the maximum element is correctly placed at \(j\), recurse on all elements except the maximum: `slowsort(A, i, j - 1)`.
Complexity
Interactive visualization
Pseudocode
procedure slowSort(A, i, j): if i >= j: return mid ← floor((i + j) / 2) slowSort(A, i, mid) slowSort(A, mid + 1, j) if A[mid] > A[j]: swap(A[mid], A[j]) slowSort(A, i, j - 1)
JavaScript implementation
function slowSort(arr, i = 0, j = arr.length - 1) { if (i >= j) return arr; const mid = Math.floor((i + j) / 2); slowSort(arr, i, mid); slowSort(arr, mid + 1, j); if (arr[mid] > arr[j]) { [arr[mid], arr[j]] = [arr[j], arr[mid]]; } slowSort(arr, i, j - 1); return arr; }
Pros and cons
Pros
- Intentionally hilarious and educational.
- Demonstrates that recursive divide-and-conquer structures can easily degenerate if not scheduled correctly.
- Operates in-place.
Cons
- Incredibly bad time complexity, worse than polynomial.
- Deep recursion stack, causing quick call stack overflows.
- Completely unusable for any real task.
When (not) to use it
- Humorous demonstrations: Perfect for coding humor or testing if someone reads code before deploying it.
- Production / standard systems: Never! Bubble sort is a speed demon compared to Slowsort.
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