How to implement the pancake sort in Java?

The pancake sort, aka burnt pancake sorting, was proposed by an American mathematician and computer scientist Jacob E. Goodman in the early 1970s. The pancake sort comes from a fun puzzle to stack differently sized pancakes, such as the biggest one at the bottom and the smallest one on the top, using a spatula to flip them. That’s how the algorithm of pancake sort works. It performs sorting by using only the flipping operation.

In the original pancake problem, the largest pancake is shifted up to the top by performing one flip and then performing a second flip to shift it to the bottom. The same process is repeated on the remaining pancakes until they all are in order. Let’s understand this with the following illustration:

Flipping pancakes to move the largest pancake to the bottom
Flipping pancakes to move the largest pancake to the bottom

How the algorithm works

The algorithm works in the following steps:

  1. It iterates the array from the length of the array backward to index 1.

  2. It finds the index of maximum value from the start to the current index of the loop.

  3. If the index of the maximum value is not equal to the current index:

    1. It performs the first flip up to the index of the maximum value to bring the maximum value to the top.

    2. It performs the second flip up to the current index to shift the maximum value to the current index.

Play the following slides to visualize the working of the pancake sort:

canvasAnimation-image
1 of 19

Pseudocode

Let’s see the pseudocode of this algorithm before implementing it in any programming language.

for currIndex from last_index_of_array to 1:
maxValueIndex = findMaxValueIndex(array, currIndex)
if maxValueIndex != currIndex:
flip(array, maxValueIndex)
flip(array, currIndex)
Pseudocode of the pancake sort algorithm

Code example

Let’s implement the code of the pancake sort using Java in the following playground:

class PancakeSort {
static void pancakeSort(int[] array){
for(int currIndex = array.length-1; currIndex>0; currIndex--){
int maxValueIndex = findMaxValueIndex(array, currIndex);
if(maxValueIndex != currIndex){
flip(array, maxValueIndex);
flip(array, currIndex);
}
}
}
static int findMaxValueIndex(int[] array, int index){
int maxIndex = 0;
for(int i = 1; i<=index; i++){
if(array[i] > array[maxIndex]){
maxIndex = i;
}
}
return maxIndex;
}
static void flip(int[] array, int index){
int start = 0;
while(start<index){
int temp = array[start];
array[start] = array[index];
array[index] = temp;
index--;
start++;
}
}
static void printArray(int[] array){
for(int element : array){
System.out.print(element + " ");
}
System.out.println();
}
public static void main( String args[] ) {
int[] array = {15, 13, 24, 7, 18, 3, 22, 9};
System.out.println("Original Array:");
printArray(array);
pancakeSort(array);
System.out.println("Sorted Array:");
printArray(array);
}
}

Code explanation

Let’s discuss the code:

  • Lines 2–11: We define the pancakeSort() function to implement the algorithm of the pancake sort in which:

    • We find the index of maximum value by the findMaxValueIndex() function and sort it in the maxValueIndex variable.

    • We flip the elements using the flip() function if maxValueIndex is not equal to the current index.

  • Lines 13–21: We define the findMaxValueIndex() function that returns the index of the maximum value. We use a for loop to find out the maximum value index.

  • Lines 23–32: We define the flip() function that flips the elements from the start of the array to the passed index value. We use a while loop to swap the elements. After performing the swapping, we increment the start variable and decrement the index variable.

  • Lines 34–39: We define a printArray() function that prints the given array to the console.

  • Lines 42–45: We create an unsorted array and print the array before sorting.

  • Lines 47–50: We call the pancakeSort() function and print the array after sorting.

Complexity

The time complexity of the pancake sort algorithm is O(n2)O(n^2).

Since the pancake sort algorithm is an in-place sorting algorithm, it doesn’t require any extra space to perform sorting. So the space complexity is O(1)O(1).

Pros and cons

Let’s discuss some pros and cons of the pancake sort.

Pros

  • Pancake sort is a simple algorithm to understand and implement.

  • It is a useful algorithm for teaching students a different technique for sorting.

  • It is an in-place sorting algorithm, which means it doesn’t require any extra space to perform sorting.

Cons

  • Pancake sort is not an efficient algorithm. It has the O(n2)O(n^2) time complexity, which means it can be very slow on large arrays.

  • It is not a stable algorithm, which means the order of equal elements is not maintained.

Optimizations

In the original algorithm, we flip the pancakes twice to place the largest pancake in its final place because we cannot flip between pancakes. Instead of making two flips, we can utilize the benefits of the array and make a single flip between the maxValueIndex and currIndex. If we use the swaps instead of flips, the algorithm will become the selection sort.

Unlock your potential: Sorting algorithms series – all in one place!

To continue your exploration of sorting algorithms, check out our series of Answers below:

Free Resources

HowDev By Educative. Copyright ©2025 Educative, Inc. All rights reserved