Sorting Pancakes, Flipping Roti Pratas
Received an interesting question for labwork, something that is built on Pancake sorting. Well, of cos there are different outputs, the one that has the least flip, selection sort of flip, selection sort with ignore type of flip (ignore as in not flipping those pratas already in place).
Limitations has been imposed on the lab, that is only Stacks are to be used. No arrays for storage and sorting. Seems like the lab is getting more interesting. Based on the specifications of the program. It seems to be O(lg n) efficient, as it tries every of the prata once in worst case scenario.
Some running result
1 9 2 8 3 7 4 6 5
1 2 3 4 5 6 7 8 9
8 1 4 2 4 3 5 4 5 5 6 1 3 9 1 8 9 1 11 10 3 1 0
Debug mode on
1 9 2 8 3 7 4 6 5
flipped: 8
current stack: 9 1 2 8 3 7 4 6 5
flipped: 1
current stack: 5 6 4 7 3 8 2 1 9
flipped: 4
current stack: 8 3 7 4 6 5 2 1 9
flipped: 2
current stack: 1 2 5 6 4 7 3 8 9
flipped: 4
current stack: 7 4 6 5 2 1 3 8 9
flipped: 3
current stack: 3 1 2 5 6 4 7 8 9
flipped: 5
current stack: 6 5 2 1 3 4 7 8 9
flipped: 4
current stack: 4 3 1 2 5 6 7 8 9
flipped: 5
current stack: 5 2 1 3 4 6 7 8 9
flipped: 5 (same as previous flip, can be removed with prev flip)
current stack: 4 3 1 2 5 6 7 8 9
flipped: 6
current stack: 2 1 3 4 5 6 7 8 9
flipped: 1
current stack: 9 8 7 6 5 4 3 1 2
flipped: 3
current stack: 3 4 5 6 7 8 9 1 2
flipped: 9
current stack: 3 4 5 6 7 8 9 1 2
flipped: 1
current stack: 2 1 9 8 7 6 5 4 3
flipped: 8
current stack: 1 2 9 8 7 6 5 4 3
flipped: 9
current stack: 1 2 9 8 7 6 5 4 3
flipped: 1
current stack: 3 4 5 6 7 8 9 2 1
flipped: 11
current stack: 3 4 5 6 7 8 9 2 1
flipped: 10
current stack: 3 4 5 6 7 8 9 2 1
flipped: 3
current stack: 9 8 7 6 5 4 3 2 1
flipped: 1
current stack: 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
8 1 4 2 4 3 5 4 5 5 6 1 3 9 1 8 9 1 11 10 3 1 0
The lab question: view it!