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
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.

Questions

References