Advanced Shaker Sort

For when regular sorting just isn't enough.

What is the Shaker Sort Algorithm?

The Shaker Sort algorithm is a variation of the Bogo Sort, but without the existential dread of never terminating.

When to Use Shaker Sort:

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

Implementation

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
  

Example Use Cases:

Sorting a collection of sentient, self-aware socks:

  1. Sort a list of socks by color:
  2. Sort a list of socks by size:
  3. Sort a list of socks by material:
See more examples of Shaker Sort in action.