tap for sound

Pancake Sort Visualization

Pancake Sort only reverses prefixes of the list: flip the largest value to the top, then flip it into place.

How it works

Pancake Sort can only do one move: flip the top k elements, like flipping a stack of pancakes with a spatula. To sort, it finds the largest unsorted value, flips it to the front, then flips the whole unsorted part so that value lands at the end. Each value needs at most two flips.

That is at most 2n flips but O(n²) comparisons and element moves, because each flip reverses many elements. It is a classic puzzle in computer science (Bill Gates co-wrote a paper on it) rather than a practical sort.

What to watch for

Watch big chunks of the picture reverse in one go as each flip lands the next largest value at the end.

Complexity

Best
n²
Average
n²
Worst
n²
Extra space
1
Stable
No

Race it

Same shuffle, same speed, one winner: Pancake vs Selection. Or open the studio to try other layouts, sizes and speeds, and make a video of it.

FAQ

How many flips does Pancake Sort need?

At most 2n − 3 with this simple method; the true minimum for the worst case is still an open problem.

Is it stable?

No. Flips reverse the order of everything they touch.