Question
You are in front of a stack of pancakes of different diameter. Unfortunately, you cannot eat them unless they are sorted according to their size,
You are in front of a stack of pancakes of different diameter. Unfortunately, you cannot eat them unless they are sorted according to their size, with the biggest one at the bottom.
To sort them, you are given a spatula that you can use to split the stack in two parts and then flip
the top part of the stack. Write the pseudo-code of a function sortPancakes that sorts the stack.
The i-th element of array pancakes contains the diameter of the i-th pancake, counting from the bottom. The sortPancakes algorithm can modify the stack only through the spatulaFlip function whose interface is specified below.
(Hint: Notice that you can move a pancake at position x to position y, without modifying the
positions of the order of the other pancakes, using a sequence of spatula flips.)
/* Flips over the stack of pancakes from position pos and returns the result */
int[] spatulaFlip(int pos, int[] pancakes);
int[] sortPancakes(int[] pancakes) {
/*Write your peudo-code here*/
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started