0

In pancake sort, the primary operation is: flip all pancakes above a given position. What about flipping all pancakes between two given positions? Anybody knows if this has been studied?

To illustrate the problem, here is a quick brute-force, greedy implementation in Python 2.7 (not quite sure it always converges though):

def sort(a):
    entropy = 0
    for i in range(len(a) - 1):
        entropy += abs(a[i] - a[i+1])
    while True:
        max_improvement = 0
        for i in range(len(a) - 1):
            for j in range(i + 1, len(a)):
                improvement = 0
                if i > 0:
                    improvement += abs(a[i] - a[i-1]) - abs(a[j] - a[i-1])
                if j < len(a) - 1:
                    improvement += abs(a[j] - a[j+1]) - abs(a[i] - a[j+1])
                if improvement > max_improvement:
                    max_improvement = improvement
                    (next_i, next_j) = (i, j)
        if max_improvement == 0:
            if a and a[0] > a[-1]:
                a[:] = a[::-1]
                print a
            return
        entropy -= max_improvement
        a[next_i:next_j+1] = a[next_i:next_j+1][::-1]
        print a

a = [7, 1, 3, 8, 6, 0, 4, 9, 2, 5]
print a
sort(a)

Output:

[7, 1, 3, 8, 6, 0, 4, 9, 2, 5]
[7, 6, 8, 3, 1, 0, 4, 9, 2, 5]
[7, 6, 8, 9, 4, 0, 1, 3, 2, 5]
[7, 6, 8, 9, 4, 5, 2, 3, 1, 0]
[9, 8, 6, 7, 4, 5, 2, 3, 1, 0]
[9, 8, 7, 6, 4, 5, 2, 3, 1, 0]
[9, 8, 7, 6, 5, 4, 2, 3, 1, 0]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Aristide
  • 3,606
  • 2
  • 30
  • 50
  • What are you exactly asking for? No or yes + a reference to a paper? Stackoverflow doesn't seem to be the place for that... – Vincent van der Weele Feb 12 '15 at 10:49
  • Yes, I would like to know is this problem is known, in which case a reference to a paper would be fantastic. As for the right place to ask that, I already skimmed the list of StackExchange sites, but couldn't find some more appropriate place than StackOverflow. Did I miss something? – Aristide Feb 12 '15 at 10:57
  • see the [help center](http://stackoverflow.com/help/on-topic): "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam." I'm not sure if any of the SE sites would be suited for this question but maybe [CS](http://cs.stackexchange.com/) comes closest. – Vincent van der Weele Feb 12 '15 at 11:10
  • I think the help center recommandation aims to prevent questions similar to "What is a good ressource to learn C++?", which would result in a flood of subjective answers. My question is "Has this problem been studied? (possible corollaries: under which name? by whom? in which paper? all of these are facts). Thanks for the pointer to CS, which actually seems a better fit. I will repost my question there. – Aristide Feb 12 '15 at 11:20

1 Answers1

0

Following a suggestion of a commenter, I cross-posted this question on CS StackExchange Beta where it quickly received the answer I was looking for: this problem is called reversal sorting.

Here is a working Python implementation.

Note: since there are no guarantees that a current SE site will live past beta, I would like to keep my question open here, where IMHO it is equally on-topic. Feel free to close it if it's contrary to the StackExchange etiquette.

Community
  • 1
  • 1
Aristide
  • 3,606
  • 2
  • 30
  • 50