Skip to main content
← All posts

Slow Sort: Mechanism, Complexity Analysis & Interactive Visualizer

Md. Harun-Or-RoshidNov 5, 20268 min readArticle
Slow Sort: Mechanism, Complexity Analysis & Interactive Visualizer
Rare & Educational · Article 54 of 62

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:

  1. To sort the sub-segment from index \(i\) to \(j\):
  2. **Left Half Recurse:** Sort the left half: `slowsort(A, i, mid)`.
  3. **Right Half Recurse:** Sort the right half: `slowsort(A, mid + 1, j)`.
  4. **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\).
  5. **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

Best time
Ω(n^(log n))
Average time
Ω(n^(log n))
Worst time
O(n^(log n))
Extra space
O(n) (stack depth)
Stable
No
In-place
Yes
Slow Sort is so slow that its best-case time complexity is **worse than polynomial time**! Its complexity is \(\Omega(n^{\frac{\log_2 n}{2}})\), which makes it slower than Stooge Sort for any reasonable size \(n\).

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.
Up next: Bogo Sort — the classic randomized sorting algorithm that randomly shuffles the array until it happens to be sorted.
Share:
Share on X
Share on LinkedIn
Share on Facebook
Share on Reddit
Share on WhatsApp
Share on Telegram
Copy link

Related reading

Slow Sort Sorting Algorithm & Guide | Draftsbook · Draftsbook