Design an efficient algorithm to sort 5 distinct keys in fewer than 8 comparisons

前端 未结 13 1307
轮回少年
轮回少年 2020-12-01 11:30

Design an efficient algorithm to sort 5 distinct - very large - keys less than 8 comparisons in the worst case. You can\'t use radix sort.

相关标签:
13条回答
  • 2020-12-01 11:43
    
    A B C D E
    
    A
    | C D E     - 1 Comparison
    B
    
    A C
    | | E       - 1 Comparison
    B D
    
      A
     / \
    B   C   E   - 1 Comparison
         \
          D
    
    

    E needs 3 comparisons. It should be compared to A, C, D

    Try A-C-D-E in that order.

    Overall there will be nine comparisons -- not very performant.

    0 讨论(0)
  • 2020-12-01 11:47

    This is pseudocode based on Beta's answer. Might have some mistakes as I did this in a hurry.

    if (A > B)
        swap A, B
    if (C > D)
        swap C, D
    if (A > C)
        swap A, C
        swap B, D  # Thanks Deqing!
    
    if (E > C)
        if (E > D)  # A C D E
            if (B > D)
                if (B > E)
                    return (A, C, D, E, B)
                else
                    return (A, C, D, B, E)
             else
                if (B < C)
                    return (A, B, C, D, E)
                else
                    return (A, C, B, D, E)
    
        else  # A C E D
            if (B > E)
                if (B > D)
                    return (A, C, E, D, B)
                else
                    return (A, C, E, B, D)
             else
                if (B < C)
                    return (A, B, C, E, D)
                else
                    return (A, C, B, E, D)
    else
        if (E < A)  # E A C D
            if (B > C)
                if (B > D)
                    return (E, A, C, D, B)
                else
                    return (E, A, C, B, D)
             else
                 return (E, A, B, C, D)
    
        else  # A E C D
            if (B > C)
                if (B > D)
                    return (A, E, C, D, B)
                else
                    return (A, E, C, B, D)
             else
                if (B < E)
                    return (A, B, E, C, D)
                else
                    return (A, E, B, C, D)
    
    0 讨论(0)
  • 2020-12-01 11:51

    Compare A to B and C to D. WLOG, suppose A>B and C>D. Compare A to C. WLOG, suppose A>C. Sort E into A-C-D. This can be done with two comparisons. Sort B into {E,C,D}. This can be done with two comparisons, for a total of seven.

    0 讨论(0)
  • 2020-12-01 11:51

    According to Wikipedia:

    Determining the exact number of comparisons needed to sort a given number of entries is a computationally hard problem even for small n, and no simple formula for the solution is known."

    Presumably this means there is no known tractable (efficient) algorithm for determining an exactly optimal comparison sort.

    0 讨论(0)
  • 2020-12-01 11:53

    It has to be 7 or more comparisons.

    There are 120 (5 factorial) ways for 5 objects to be arranged. An algorithm using 6 comparisons can only tell apart 2^6 = 64 different initial arrangements, so algorithms using 6 or less comparisons cannot sort all possible inputs.

    There may be a way to sort using only 7 comparisons. If you only want to sort 5 elements, such an algorithm could be found (or proved not to exist) by brute force.

    0 讨论(0)
  • 2020-12-01 11:56

    Others have stated that there are 5! = 120 arrangements (permutations) to handle, so you need 7 comparisons. To identify the permutation, in principle, you can construct a big nested if statement 7 comparisons deep. Having identified the permutation, a precalculated swap/rotation sequence can be applied.

    The first problem is that the choice of second comparison depends on the result of the first comparison and so on. The trick at each stage is to choose a good comparison to divide the current set of possible permutations into two equal subsets. Simplest approach - evaluate the split that each comparison would achieve until you find a suitably balanced one. Exit early if you find a perfect balance, but be aware that perfect balance won't always be possible as we don't have exactly 2^7=128 permutations - in some (I assume 8) cases, we only need six comparisons.

    The second problem is designing the swap/rotation sequences for each of the 120 possible permutations, and that's probably a dynamic programming thing. Probably requires recursive search of an if-I-do-this, the next result is that, then recurse "game tree", and you should really cache intermediate results IOW. Too tired to figure out the details ATM, sorry.

    You might put all the steps into a digraph that fans out (identifying the permutation), then fans back in (applying each reordering step). Then, probably run it through a digraph minimisation algorithm.

    Wrap this up in a code generator and you're done - your own algorithmically near-perfect 5 item sorter. The digraph stuff kind of implies gotos in the generated code (esp. if you minimise), but people tend to turn a blind eye to that in generated code.

    Of course all this is a bit brute force, but why bother with elegance and efficiency - odds are you'll only run the working generator once anyway, and the problem size is small enough to be achievable (though probably not if you do independent naive "game tree" searches for each permutation).

    0 讨论(0)
提交回复
热议问题