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
- The only operation allowed is a flip: reverse the first k items (like flipping a stack of pancakes with a spatula).
- Find the largest unsorted pancake.
- Flip it to the top, then flip the whole unsorted stack so it lands at the bottom.
- Shrink the unsorted part by one and repeat. At most 2(n−1) flips.
Watch it run
Loading animation…