[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference rusure::math

Title:Mathematics at DEC
Moderator:RUSURE::EDP
Created:Mon Feb 03 1986
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:2083
Total number of notes:14613

1973.0. "Sort by Prefix Reversal" by RUSURE::EDP (Always mount a scratch monkey.) Thu May 25 1995 10:04

    From the net (David Rodriguez, [email protected], who doesn't say
    where he got it):
    
    How many operations are required to sort any string of length n, where
    each operation is a reversal of the first k characters?
    
    Thus reversing the first 3 characters of 2413 yields 1423.  Rodriguez
    reports a claim this never requires more than n operations.  Is that
    true?
    
    Is there a good algorithm for figuring out the fewest operations for a
    particular string?
    
    
    				-- edp
    
    
Public key fingerprint:  8e ad 63 61 ba 0c 26 86  32 0a 7d 28 db e7 6f 75
To find PGP, read note 2688.4 in Humane::IBMPC_Shareware.
T.RTitleUserPersonal
Name
DateLines
1973.1EVMS::HALLYBFish have no concept of fireThu May 25 1995 10:4619
    If I understand this right it is clear that 2n is an upper bound on
    the number of operations. For any string of length n you can move the
    largest element to the end in 2 operations and thus reduce the problem
    to sorting a string of length n-1.
    
    E.g., given the string 27164 you bring the 7 to the front: 72164 and
    then reverse the whole string: 46127. Now you only need sort 4612 and
    by the same token that takes 2 steps to yield 2146, and so on.
    
    You can save a step or two in the above anytime you have two position-
    adjacent elements that are also order-adjacent. E.g., 14762 becomes
    67412 then 76412 then 21467, ordering two elements in 3 steps. Had it
    been 14672 you do even better: 76412, 21467 -- only 2 steps.
    
    It certainly seems difficult to come up with a counterexample;
    "adjacancies" keep cropping up. It would seem to me that coming up with
    an algorithm for minimizing reversals would involve maximizing adjacencies.
    
      John
1973.2HANNAH::OSMANsee HANNAH::IGLOO$:[OSMAN]ERIC.VT240Thu May 25 1995 10:5936
   
>    How many operations are required to sort any string of length n, where
>    each operation is a reversal of the first k characters?
  

As written, for any k < n, then *no* number of operations will work.

For example, consider this string:

	4321

That's n=4.

Consider k=3.

Reverse:

	2341

Reverse:

	4321

Reverse:

	2341

Bored yet ?

Clearly we'll never produce 1234 with k=3.

Am I misreading, or is problem mistated ?  Help !

Thanks.

/Eric
1973.3pointerHERON::BUCHANANEt tout sera bien etThu May 25 1995 11:193
This is addressed in 425.*

Andrew.
1973.4AUSSIE::GARSONachtentachtig kacheltjesThu May 25 1995 22:5314
    re .2
    
    Your interpretation is a reasonable literal interpretation but not I
    think the one intended by the originator of the problem.
    
    The number of characters involved in each reversal is not a constant.
    That is, I can reverse the first 5 (say) characters on the first
    reversal and then reverse the first 2 (say) characters on the second
    reversal.
    
    "How many operations are required to sort any string of length n, where
    each operation is a reversal of some prefix of the string." may come
    closer to the intended meaning.
    
1973.5RTL::GILBERTFri May 26 1995 16:515
.0> Rodriguez reports a claim this never requires more than n operations.
.0> Is that true?

    No.  The table in 425.4 shows that for n in {4,5}, n flips may be required;
    and for n in {6,7,8}, n+1 flips may be required.