Skip to content
StackDecode
SortingAnimated

Pancake Sort

Sort using only prefix reversals.

Time

O(n²)

Extra space

O(1)

Use it for

Puzzle and theory; the flip is the only operation.

How it works

  1. The only operation allowed is a flip: reverse the first k items (like flipping a stack of pancakes with a spatula).
  2. Find the largest unsorted pancake.
  3. Flip it to the top, then flip the whole unsorted stack so it lands at the bottom.
  4. Shrink the unsorted part by one and repeat. At most 2(n−1) flips.

Watch it run

Loading animation…