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:
The algorithm works in the following steps:
It iterates the array from the length of the array backward to index 1.
It finds the index of maximum value from the start to the current index of the loop.
If the index of the maximum value is not equal to the current index:
It performs the first flip up to the index of the maximum value to bring the maximum value to the top.
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:
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)
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);}}
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.
The time complexity of the pancake sort algorithm is
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
Let’s discuss some pros and cons of the pancake sort.
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.
Pancake sort is not an efficient algorithm. It has the
It is not a stable algorithm, which means the order of equal elements is not maintained.
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:
What are sorting algorithms?
Understand the fundamentals of sorting algorithms and their role in organizing data efficiently.
What is tree sort?
Learn how tree-based structures can be used to sort elements efficiently.
What is Bitonic sort?
Discover Bitonic Sort, a parallel sorting algorithm ideal for hardware implementations.
What is Flash Sort?
Explore Flash Sort, a hybrid sorting technique designed for fast performance on large datasets.
How to implement the pigeonhole sorting algorithm in Python
A step-by-step guide to implementing Pigeonhole Sort in Python for efficient data sorting.
How to implement comb sort in C++
Learn how to implement Comb Sort in C++ to improve sorting efficiency over Bubble Sort.
Implementation of the cocktail sort in C++
Understand how Cocktail Sort refines Bubble Sort for bidirectional sorting in C++.
How to implement cocktail sort in Python
Implement Cocktail Sort in Python to enhance sorting performance with a two-way pass.
How to implement Gnome sort in C++
Explore the simplicity of Gnome Sort and its implementation in C++.
How to implement pancake sort in Java?
Learn how to implement Pancake Sort in Java, inspired by flipping pancakes to sort stacks.
Comparison of linear-time sorting algorithms
Analyze and compare different linear-time sorting algorithms to understand their efficiency.
Free Resources