Procrastination Sort is a joke algorithm that behaves like this:
- Check whether the list is already sorted.
- If not sorted, do nothing for a random amount of time.
- Occasionally perform a random swap out of guilt.
- Rarely make accidental useful progress.
- Near deadline, panic and finally sort by repeated adjacent inversion fixes.
It is intentionally inefficient, but the visualization makes the behavior easy to understand.
The implementation is event-driven. Instead of just returning a sorted list, it yields animation snapshots with metadata (action text, step count, waits, swaps, and sorted status).
- Copy the input list.
- Emit one snapshot: "Staring at the list... building motivation."
Repeat while step < max_steps:
- If list is sorted, stop successfully.
- Pick random wait ticks in
[min_wait_ticks, max_wait_ticks]. - For each wait tick:
- Increase
stepandtotal_wait. - Emit a snapshot: "Procrastinating..."
- Increase
- Try to do something:
- With probability
guilt_probability(default0.65), swap two random indices. - Measure whether this reduced inversion count (a "helpful" guilt swap).
- Emit a snapshot highlighting the swapped bars.
- With probability
- If no guilt swap happened, maybe do accidental productivity:
- With probability
productive_nudge_probability(default0.20), find the first adjacent inversion and swap it. - Emit snapshot with highlights.
- With probability
- If neither action happened, emit "Opened another tab instead of sorting."
When max_steps is reached without sorting:
- Repeatedly find first adjacent inversion.
- Swap that pair.
- Continue up to
panic_steps(default400) or until sorted.
This behaves like a focused adjacent-inversion repair pass and is what usually guarantees convergence.
Emit one final state with:
- "Sorted at the last moment..." if sorted.
- "Still not sorted. Maybe tomorrow." if panic limit was insufficient.
- Random guilt swaps alone do not guarantee monotonic progress.
- The panic phase repeatedly fixes inversions left-to-right.
- Every such fix reduces disorder locally, and repeated passes eventually eliminate all inversions (within the panic limit).
So the algorithm is mostly chaos, then deterministic urgency.
This is not a practical sorter.
- During guilt swaps, inversion counting is used before/after each random swap.
- Inversion counting is
O(n^2). - Waiting/action cycles can be many.
- Panic mode can run up to
panic_steps, each step doing a linear scan for first inversion (O(n)).
Overall runtime is dominated by random-loop behavior and can be very large relative to standard sorting algorithms.
Color semantics:
- Blue bars: normal unsorted state.
- Orange bars: most recent swapped indices.
- Green bars: list currently sorted.
Status box fields:
Action: what the algorithm is doing right now.Step: total algorithm steps taken.Wait Ticks: time spent procrastinating.Guilt Swaps: number of random guilt swaps.Helpful Swaps: guilt swaps that reduced inversion count.Sorted: current sorted check result.
What this image shows:
- Bars are strictly non-decreasing left to right, so the list is sorted.
- All bars are green, matching
Sorted: Yes. - The status shows
Step: 700andWait Ticks: 528, meaning most effort was delay, not useful work. Guilt Swaps: 43withHelpful Swaps: 23means around half of guilt swaps helped and half were wasted/noisy.- The action line "Sorted at the last moment" confirms completion happened near the end of allowed steps.
In short: this perfectly matches the intended personality of Procrastination Sort: long delay, chaotic partial progress, and eventual finish.
pip install -r requirements.txtpython procrastination_sort_visualizer.pypython procrastination_sort_visualizer.py --size 30 --seed 42 --speed 90
python procrastination_sort_visualizer.py --values 9,4,7,1,8,2 --min-wait 2 --max-wait 8--size: random list size if--valuesis not provided.--seed: random seed for reproducible generated input.--speed: frame delay in milliseconds (smaller = faster animation).--min-wait: minimum procrastination ticks each cycle.--max-wait: maximum procrastination ticks each cycle.--values: explicit comma-separated input list.
-
Increase waiting for peak procrastination:
python procrastination_sort_visualizer.py --min-wait 8 --max-wait 20
-
Provide custom hard input:
python procrastination_sort_visualizer.py --values 50,1,49,2,48,3,47,4,46,5
-
Faster playback for long runs:
python procrastination_sort_visualizer.py --speed 50
