3

We have N numbers in a stack and we want to sort them with minimum number of operations. The only available operation is reversing last K numbers in top of the stack (K can be between 2 and N).

For example to sort sequence "2 3 4 5 1", we need 2 steps:

2 3 4 5 1 ---> 1 5 4 3 2 ---> 1 2 3 4 5

Is there any polynomial algorithm to find minimum number of steps needed?

Jayantha Lal Sirisena
  • 21,216
  • 11
  • 71
  • 92
Ali
  • 6,808
  • 3
  • 37
  • 47

2 Answers2

4

I think you are talking about the famous Pancake sorting algorithm.

Quoting from wikipedia : "The maximum number of flips required to sort 
any stack of n pancakes has been shown to lie between (15/14)n and (18/11)n,
but the exact value is not known. The simplest pancake sorting algorithm requires
at most 2n−3 flips. 

In this algorithm, a variation of selection sort, we bring the largest pancake
not yet sorted to the top with one flip, and then take it down to its final 
position with  one more, then repeat this for the remaining pancakes.

Note that we do not count the time needed to find the largest pancake, only the
number of flips; if we wished to create a real machine to execute this algorithm
in linear time, it would have to both perform prefix reversal (flips) and be
able to find the maximum of a range of consecutive numbers in constant time"
akanksha1105
  • 338
  • 2
  • 7
1

It can be done in 2N-3 steps (worst case)

Find the position of '1'

Shuffle it to the end (one step)

Shuffle it to the beginning (reverse all N)

Find the position of 2

Shuffle to the end

Shuffle to the beginning (reverse last N-1)

Repeat...

When you get to consider element N-1, it is either already in the right place, or at the end. Worst case you need one more reversal to finish. This gives you 2N-3.

It is possible you can do better for a given sequence when you take advantage of some intrinsic order. I have a hunch that an initial step that maximizes the "order" of elements might be good- that is, do an initial step such that the "number of elements that have all elements smaller than them to their left" is greatest. For example, starting with 43215, an initial complete reversal gives 51234 (order number =3), after which my algorithm gets the correct order in just two steps. I'm not sure if this is general.

Floris
  • 45,857
  • 6
  • 70
  • 122
  • When you do these steps, if you find the next number is already in place you subtract two from your answer; if its already in the last place you subtract one. I guess I don't understand your question? Do you suspect one can do even better? – Floris Mar 08 '13 at 03:36
  • for the sequence "4 3 2 1 5" (5 is on top), your algorithm gives 4 step. but it can be sorted in 3 step: 4 3 2 1 5 ---> 5 1 2 3 4 ----> 5 4 3 2 1 ---> 1 2 3 4 5 – Ali Mar 08 '13 at 03:41
  • Fair point. I have a hunch that you could look for the reversal that puts the most digits in the right order (your step 1), after which my algorithm works. It turns out that the worst case is actually 2N-3 because at the last steps the worst you can do it to have two element in the wrong order - 54 instead of 45. And that takes only one operation to fix – Floris Mar 08 '13 at 04:03