Bubble Pancakes

This problem is similar to the previous: you have to sort the pancakes, the smallest at the top and the biggest at the bottom, except you can only flip them to sort.

The purpose here is to sort them using the bubble algorithm, where one pancake is moved to the bottom until it encounters a bigger one. In graphical representations, it looks like a bubble takes the element and move them through the pile. This is where its name comes from.

Now, using pancakes makes it a little more difficult to write, because you have to find a way to swap only two pancakes at a time.

The bubble algorithm consists in browsing the whole pile from top to bottom, and making a comparision between two pancakes. Each time the top pancake is bigger than the one below, you have to swap them, then compare the bigger pancake you just swapped with one below, and so on. You have to browse through the entire pile until no swap has occurred, in which case the pile is sorted.

If you don't understand the basic bubble sorting algorithm, you should try the exercises within the “Sorting algorithms” lesson. If you do, but can't figure a way to swap only two pancakes at a time, think of a situation where only two pancakes are swapped at a time, and find a way to reach that situation.