Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions

Question

1. Explain the 2nd world war. 2. Who is the father of history?

Answered: 1 week ago