Published by Dimitris Kougioumtzis · Updated
Flip prefixes to move the largest remaining value to its final position.
Flip the active maximum to the front, then flip it to the end, and shrink the active prefix.
When to use Pancake sort
Sorting with prefix reversal as the permitted operation.
Choosing an alternative
Selection sort places extrema with direct exchanges.
Worked example: input and state changes
Example input
38, 12, 65, 8, 43, 27, 91, 19
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Initial state.
State at this checkpoint
{ "values": [ 38, 12, 65, 8, 43, 27, 91, 19 ], "active": [] }Step 13: Flip prefix ending at 7.
State at this checkpoint
{ "values": [ 19, 38, 12, 8, 65, 43, 27, 91 ], "active": [ 2, 5 ] }Step 26: Find the largest remaining value.
State at this checkpoint
{ "values": [ 27, 43, 8, 19, 38, 12, 65, 91 ], "active": [ 0, 1 ] }Step 39: Flip prefix ending at 1.
State at this checkpoint
{ "values": [ 38, 12, 19, 8, 27, 43, 65, 91 ], "active": [ 0, 1 ] }Step 52: Complete: values are in ascending order.
State at this checkpoint
{ "values": [ 8, 12, 19, 27, 38, 43, 65, 91 ], "active": [] }
Expected final state
Complete: values are in ascending order.
Complexity and input bounds
Family: Sorting. Time complexity: O(n²).
Space complexity: O(1) in-place reversal state.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
2ā24 comma-separated integers between 0 and 99.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
A flip reverses every position in the prefix, not merely its endpoints.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // Sorts a in place. n is the array length. Return value 0 indicates completion. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(a, n) { for (let end = n - 1; end > 0; end--) { let maximum = 0; for (let j = 1; j <= end; j++) { if (a[j] > a[maximum]) { maximum = j; } } if (maximum == end) { continue; } for (let pass = 0; pass < 2; pass++) { let right = maximum; if (pass == 1) { right = end; } let left = 0; while (left < right) { let t = a[left]; a[left] = a[right]; a[right] = t; left++; right--; } } } return 0; } |
Source
The paper analyzes prefix-reversal sorting. The playground shows a simple maximum-to-front prefix-flip strategy, not all results in the paper.
- Bounds for sorting by prefix reversal
William H. Gates; Christos H. Papadimitriou (1979). Related research paper.