Chris is famous for baking perfectly round pancakes but they all come in different sizes. The customers usually don't care as long as its delicious, but today this one customer demands the pancakes comes in order, that is the smallest one being the top of the stack.
Chris is very annoyed but he will do it anyway. After baking a stack of pancakes, he will use a spatula to flip some top section of the pancake. For clarity, see the illustration below
sorting pancake with a spatula
Your task is to sort the pancakes. First, you keep track of 1 stack PANCAKE and 1 queue ORDERED. While PANCAKE is not empty, perform the following
Flip the biggest size to the top.
Remove the top of PANCAKE away and push to ORDERED.
Flip PANCAKE
Let the number of flips you need to perform be . What is the complexity of this algorithm?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
No explanations have been posted yet. Check back later!