Pancake sort: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

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.

Open the 3D visualization

Pancake sort example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

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.

  1. Step 0: Initial state.

    State at this checkpoint
    {
      "values": [
        38,
        12,
        65,
        8,
        43,
        27,
        91,
        19
      ],
      "active": []
    }
  2. Step 13: Flip prefix ending at 7.

    State at this checkpoint
    {
      "values": [
        19,
        38,
        12,
        8,
        65,
        43,
        27,
        91
      ],
      "active": [
        2,
        5
      ]
    }
  3. Step 26: Find the largest remaining value.

    State at this checkpoint
    {
      "values": [
        27,
        43,
        8,
        19,
        38,
        12,
        65,
        91
      ],
      "active": [
        0,
        1
      ]
    }
  4. Step 39: Flip prefix ending at 1.

    State at this checkpoint
    {
      "values": [
        38,
        12,
        19,
        8,
        27,
        43,
        65,
        91
      ],
      "active": [
        0,
        1
      ]
    }
  5. 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:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

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