Algorithm description
Assuming that there are n pancakes stacked together in the frying pan, they are different in size and inconsistent in order, we need to turn them over by a shovel, sort them out, and finally get a big pancake at the bottom and a small pancake sequence on top. This sort of process is the sort of pancake.
This algorithm has two kinds of solutions, one is the common solution and the other is the optimal solution.
General argumentation:
For example your initial pancake order is [2,4,3,1]
Then 2 and 4 swap positions, then 4 and 1 swap positions, to draw [1,3,2,4].
Then 3 and 1 swap positions, followed by 3 and 2 swap positions, to draw [2,1,3,4].
The last 2 and 1 swap positions, resulting in [1,2,3,4]
Through the process of ordinary solution, we can make a summary of the algorithm:
We actually each time is 22 compares the pancake, then first puts the big pancake to the top, then puts the big pancake to the bottom. If it's n pancakes, then we're going to do it 2*n times. But let's take a look. When we switched between 2 and 1, we only did it once, that is, when there is only the last set of numbers, we only need to sort once, so we have n-2 +1, so the final result is 2n-3
Of course, our time complexity is only sort, and the time complexity of the query is not counted.
The best solution argument:
The best solution found is (15/14) n≦f (n) ≦ (5n+5)/3
But did not find the proof of the information, and I guess I do not understand, so I do not come to the argument, issued to record, after the ability to understand and then come back here to add
Summarize:
The pancake sort feeling is not very practical for our actual application scenario, just open up the idea.
Code
Because the examination, so for the time not to write, later to fill in
Reference
http://semoncat.github.io/blog/2012/11/05/pancake-sorking/
http://blog.jobbole.com/74263/
Hark data structure and algorithm practice pancake sorting