Pancake sort restricts every move to a prefix reversal. To place the largest value in the unsorted prefix, it first flips that value to index 0, then flips the whole live prefix so the value lands at its final right boundary. The boundary shrinks and the process repeats.
The fixed suffix is the invariant: after finishing boundary end, every position after end contains its final value and is excluded from later flips. The restriction makes the algorithm useful for studying move-constrained sorting, although repeated maximum scans revisit the live prefix for every boundary.
Visualization
Each move reverses [0..k]. When the live maximum is not already at the boundary, one flip brings it to the front and a second carries it to the boundary. At most two prefix reversals settle each position.
Complexity
Pancake Sort complexity
n
number of elements in the array
Boundary and implementation
The direct algorithm mutates the same array but is unstable because a prefix reversal can move equal keys across each other. It scans every shrinking prefix to locate the maximum even if no flips are needed. A preliminary sortedness check can return early, but it does not improve the general mechanism.
C# implementation
public static void PancakeSort(int[] values){ for (var end = values.Length - 1; end > 0; end--) { var max = 0; for (var i = 1; i <= end; i++) if (values[i] > values[max]) max = i; if (max == end) continue; if (max > 0) Flip(values, max); Flip(values, end); }}private static void Flip(int[] values, int end){ for (var left = 0; left < end; left++, end--) (values[left], values[end]) = (values[end], values[left]);}
No arbitrary swap occurs: every mutation is part of a prefix reversal.
Questions
Why can two flips place the live maximum?
The first moves the maximum from its current index to the front; the second reverses the full live prefix and moves the front value to its right boundary.
Why is the common implementation unstable?
Reversing a prefix reverses the relative order of every pair in it, including equal keys.