What is the fastest sorting algorithm for a small number of integers?

后端 未结 12 3219
青春惊慌失措
青春惊慌失措 2021-02-20 18:45

I am wondering what the fastest algorithm would be for this. I have 8 integers between 0 and 3000 and I need to sort them. Although there are only 8 integers, this operation w

12条回答
  •  Happy的楠姐
    2021-02-20 19:10

    Years later) for up to 32 inputs, see the Sorting network generator. For 8 inputs, it gives 19 swaps, like Sven Marnach's answer:

    o--^--^--------^--------------------------o
       |  |        |
    o--v--|--^--^--|--^--^--------------------o
          |  |  |  |  |  |
    o--^--v--|--v--|--|--|--^--------^--------o
       |     |     |  |  |  |        |
    o--v-----v-----|--|--|--|--^--^--|--^--^--o
                   |  |  |  |  |  |  |  |  |
    o--^--^--------v--|--v--|--|--|--v--|--v--o
       |  |           |     |  |  |     |
    o--v--|--^--^-----v-----|--|--|-----v-----o
          |  |  |           |  |  |
    o--^--v--|--v-----------v--|--v-----------o
       |     |                 |
    o--v-----v-----------------v--------------o
    
    
    There are 19 comparators in this network,
    grouped into 7 parallel operations.
    
    [[0,1],[2,3],[4,5],[6,7]]
    [[0,2],[1,3],[4,6],[5,7]]
    [[1,2],[5,6],[0,4],[3,7]]
    [[1,5],[2,6]]
    [[1,4],[3,6]]
    [[2,4],[3,5]]
    [[3,4]]
    

提交回复
热议问题