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

后端 未结 12 2921
青春惊慌失措
青春惊慌失措 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条回答
  •  故里飘歌
    2021-02-20 19:18

    The fastest way is a sorting network implemented in hardware. Barring that, the fastest way is determined only by measuring. I'd try

    • std::sort,
    • pigeonhole (bucket) sort with reuse of the buckets,
    • a bunch of if statements, and
    • insertion sort

    in that order, because it's the easiest-to-hardest order (try to get insertion sort right the first time...) until you find something that's maintainable once the constant eight turns out to have the value nine.

    Also, bubble sort, selection deserve and shell sort deserve notice. I've never actually implemented those because they have bad rep, but you could try them.

提交回复
热议问题