For when regular sorting just isn't enough.
The Shaker Sort algorithm is a variation of the Bogo Sort, but without the existential dread of never terminating.
1. When you want to spend an eternity in an infinite loop, wondering if it will ever work.
2. When your users are too polite to ask for help, but still want their socks sorted in a vaguely coherent order.
Learn more about when to use Shaker Sort.See also: Related Algorithms
Here's a simple implementation in Python:
def shaker_sort(arr):
while True:
swapped = False
for i in range(len(arr) - 1):
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
swapped = True
if not swapped:
break
return arr
Sorting a collection of sentient, self-aware socks: