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

前端 未结 13 1306
轮回少年
轮回少年 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:59

    Five item can be sorted with seven comparisons in the worst cast because log2(5!) = 6.9. I suggest to check if any standard sort sort algorithm achieves this number - if not it should be quite easy to hard-code a comparison sequence because of the low number of required comparisons.

    I suggest to write a program to find the comparison sequence. Create a list with all 120 permutations of the numbers one to five. Then try all ten possible comparisons and select that one, that splits the list as good as possible in two equal sized lists. Perform this split and apply the same procedure to two lists recursively.

    I wrote a small program to do this and here is the result.

    Comparison 1: 0-1 [60|60] // First comparison item 0 with item 1, splits case 60/60
    Comparison 2: 2-3 [30|30] // Second comparison for the first half of the first comparison
    Comparison 3: 0-2 [15|15] // Third comparison for the first half of the second comparison for the first half of first comparison
    Comparison 4: 2-4 [8|7]
    Comparison 5: 3-4 [4|4]
    Comparison 6: 1-3 [2|2]
    Comparison 7: 1-2 [1|1]
    Comparison 7: 1-4 [1|1]
    Comparison 6: 1-4 [2|2]
    Comparison 7: 1-2 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 5: 0-4 [4|3]
    Comparison 6: 1-2 [2|2]
    Comparison 7: 1-4 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 6: 1-2 [1|2]
    Comparison 7: 1-3 [1|1]
    Comparison 4: 0-4 [8|7]
    Comparison 5: 1-4 [4|4]
    Comparison 6: 1-3 [2|2]
    Comparison 7: 3-4 [1|1]
    Comparison 7: 0-3 [1|1]
    Comparison 6: 3-4 [2|2]
    Comparison 7: 0-3 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 5: 0-3 [4|3]
    Comparison 6: 1-3 [2|2]
    Comparison 7: 2-4 [1|1]
    Comparison 7: 2-4 [1|1]
    Comparison 6: 2-4 [2|1]
    Comparison 7: 3-4 [1|1]
    Comparison 3: 0-3 [15|15] // Third comparison for the second half of the second comparison for the first half of first comparison
    Comparison 4: 3-4 [8|7]
    Comparison 5: 2-4 [4|4]
    Comparison 6: 1-2 [2|2]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 1-4 [1|1]
    Comparison 6: 1-4 [2|2]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 1-2 [1|1]
    Comparison 5: 0-4 [4|3]
    Comparison 6: 1-3 [2|2]
    Comparison 7: 1-4 [1|1]
    Comparison 7: 1-2 [1|1]
    Comparison 6: 1-2 [2|1]
    Comparison 7: 1-3 [1|1]
    Comparison 4: 0-4 [8|7]
    Comparison 5: 1-4 [4|4]
    Comparison 6: 1-2 [2|2]
    Comparison 7: 2-4 [1|1]
    Comparison 7: 0-2 [1|1]
    Comparison 6: 2-4 [2|2]
    Comparison 7: 0-2 [1|1]
    Comparison 7: 1-2 [1|1]
    Comparison 5: 0-2 [4|3]
    Comparison 6: 1-2 [2|2]
    Comparison 7: 3-4 [1|1]
    Comparison 7: 3-4 [1|1]
    Comparison 6: 2-4 [1|2]
    Comparison 7: 3-4 [1|1]
    Comparison 2: 2-3 [30|30] // Second comparison for the second half of the first comparison
    Comparison 3: 0-3 [15|15]
    Comparison 4: 0-4 [7|8]
    Comparison 5: 0-2 [3|4]
    Comparison 6: 2-4 [2|1]
    Comparison 7: 3-4 [1|1]
    Comparison 6: 1-2 [2|2]
    Comparison 7: 3-4 [1|1]
    Comparison 7: 3-4 [1|1]
    Comparison 5: 1-4 [4|4]
    Comparison 6: 2-4 [2|2]
    Comparison 7: 1-2 [1|1]
    Comparison 7: 0-2 [1|1]
    Comparison 6: 1-2 [2|2]
    Comparison 7: 0-2 [1|1]
    Comparison 7: 2-4 [1|1]
    Comparison 4: 3-4 [7|8]
    Comparison 5: 0-4 [3|4]
    Comparison 6: 1-2 [1|2]
    Comparison 7: 1-3 [1|1]
    Comparison 6: 1-3 [2|2]
    Comparison 7: 1-2 [1|1]
    Comparison 7: 1-4 [1|1]
    Comparison 5: 2-4 [4|4]
    Comparison 6: 1-4 [2|2]
    Comparison 7: 1-2 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 6: 1-2 [2|2]
    Comparison 7: 1-4 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 3: 0-2 [15|15]
    Comparison 4: 0-4 [7|8]
    Comparison 5: 0-3 [3|4]
    Comparison 6: 2-4 [1|2]
    Comparison 7: 3-4 [1|1]
    Comparison 6: 1-3 [2|2]
    Comparison 7: 2-4 [1|1]
    Comparison 7: 2-4 [1|1]
    Comparison 5: 1-4 [4|4]
    Comparison 6: 3-4 [2|2]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 0-3 [1|1]
    Comparison 6: 1-3 [2|2]
    Comparison 7: 0-3 [1|1]
    Comparison 7: 3-4 [1|1]
    Comparison 4: 2-4 [7|8]
    Comparison 5: 0-4 [3|4]
    Comparison 6: 1-2 [2|1]
    Comparison 7: 1-3 [1|1]
    Comparison 6: 1-2 [2|2]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 1-4 [1|1]
    Comparison 5: 3-4 [4|4]
    Comparison 6: 1-4 [2|2]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 1-2 [1|1]
    Comparison 6: 1-3 [2|2]
    Comparison 7: 1-4 [1|1]
    Comparison 7: 1-2 [1|1]
    

    But now the question is how to implement this in an efficient way. Maybe one could use a look-up table to store the comparison sequence. I am also not sure how to derive the ordered output from this comparison sequence in an efficient way.

    Sorting the result from above by the comparison reveals an obvious structure for the first comparisons, but it becomes harder with increasing comparison number. All blocks are symmetric around the middle indicated by -----.

    Comparison 1: 0-1 [60|60]
    
    Comparison 2: 2-3 [30|30]
    Comparison 2: 2-3 [30|30]
    
    Comparison 3: 0-2 [15|15]
    Comparison 3: 0-3 [15|15]
    -----
    Comparison 3: 0-3 [15|15]
    Comparison 3: 0-2 [15|15]
    
    Comparison 4: 2-4 [8|7]
    Comparison 4: 0-4 [8|7]
    Comparison 4: 3-4 [8|7]
    Comparison 4: 0-4 [8|7]
    -----
    Comparison 4: 0-4 [7|8]
    Comparison 4: 3-4 [7|8]
    Comparison 4: 0-4 [7|8]
    Comparison 4: 2-4 [7|8]
    
    Comparison 5: 3-4 [4|4]
    Comparison 5: 0-4 [4|3]
    Comparison 5: 1-4 [4|4]
    Comparison 5: 0-3 [4|3]
    Comparison 5: 2-4 [4|4]
    Comparison 5: 0-4 [4|3]
    Comparison 5: 1-4 [4|4]
    Comparison 5: 0-2 [4|3]
    -----
    Comparison 5: 0-2 [3|4]
    Comparison 5: 1-4 [4|4]
    Comparison 5: 0-4 [3|4]
    Comparison 5: 2-4 [4|4]
    Comparison 5: 0-3 [3|4]
    Comparison 5: 1-4 [4|4]
    Comparison 5: 0-4 [3|4]
    Comparison 5: 3-4 [4|4]
    
    Comparison 6: 1-3 [2|2]
    Comparison 6: 1-4 [2|2]
    Comparison 6: 1-2 [2|2]
    Comparison 6: 1-2 [1|2]
    Comparison 6: 1-3 [2|2]
    Comparison 6: 3-4 [2|2]
    Comparison 6: 1-3 [2|2]
    Comparison 6: 2-4 [2|1]
    Comparison 6: 1-2 [2|2]
    Comparison 6: 1-4 [2|2]
    Comparison 6: 1-3 [2|2]
    Comparison 6: 1-2 [2|1]
    Comparison 6: 1-2 [2|2]
    Comparison 6: 2-4 [2|2]
    Comparison 6: 1-2 [2|2]
    Comparison 6: 2-4 [1|2]
    -----
    Comparison 6: 2-4 [2|1]
    Comparison 6: 1-2 [2|2]
    Comparison 6: 2-4 [2|2]
    Comparison 6: 1-2 [2|2]
    Comparison 6: 1-2 [1|2]
    Comparison 6: 1-3 [2|2]
    Comparison 6: 1-2 [2|2]
    Comparison 6: 1-4 [2|2]
    Comparison 6: 2-4 [1|2]
    Comparison 6: 1-3 [2|2]
    Comparison 6: 3-4 [2|2]
    Comparison 6: 1-3 [2|2]
    Comparison 6: 1-2 [2|1]
    Comparison 6: 1-2 [2|2]
    Comparison 6: 1-4 [2|2]
    Comparison 6: 1-3 [2|2]
    
    Comparison 7: 1-2 [1|1]
    Comparison 7: 1-4 [1|1]
    Comparison 7: 1-2 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 1-4 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 3-4 [1|1]
    Comparison 7: 0-3 [1|1]
    Comparison 7: 0-3 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 2-4 [1|1]
    Comparison 7: 2-4 [1|1]
    Comparison 7: 3-4 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 1-4 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 1-2 [1|1]
    Comparison 7: 1-4 [1|1]
    Comparison 7: 1-2 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 2-4 [1|1]
    Comparison 7: 0-2 [1|1]
    Comparison 7: 0-2 [1|1]
    Comparison 7: 1-2 [1|1]
    Comparison 7: 3-4 [1|1]
    Comparison 7: 3-4 [1|1]
    Comparison 7: 3-4 [1|1]
    -----
    Comparison 7: 3-4 [1|1]
    Comparison 7: 3-4 [1|1]
    Comparison 7: 3-4 [1|1]
    Comparison 7: 1-2 [1|1]
    Comparison 7: 0-2 [1|1]
    Comparison 7: 0-2 [1|1]
    Comparison 7: 2-4 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 1-2 [1|1]
    Comparison 7: 1-4 [1|1]
    Comparison 7: 1-2 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 1-4 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 3-4 [1|1]
    Comparison 7: 2-4 [1|1]
    Comparison 7: 2-4 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 0-3 [1|1]
    Comparison 7: 0-3 [1|1]
    Comparison 7: 3-4 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 1-4 [1|1]
    Comparison 7: 1-3 [1|1]
    Comparison 7: 1-2 [1|1]
    Comparison 7: 1-4 [1|1]
    Comparison 7: 1-2 [1|1]
    
    0 讨论(0)
提交回复
热议问题