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

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条回答
• For only 8 integers and given that the range is much greater than 8, insertion sort is probably the best. Try it to start with, and if profiling indicates that it's not the bottleneck then leave it.

(Depending on many factors, the cutoff point at which quick-sort becomes better than insertion sort is usually between 5 and 10 items).

讨论(0)
• The fastest would be to simply write a lot of `if` statements to compare them to determine their exact order. That will remove the overhead that any sorting algoritm has.

讨论(0)
• 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]]
``````
讨论(0)
• Have you profiled your code to show that the sort is a bottleneck? If it isn't a bottleneck, then speeding it up won't buy you much. Sorting eight short integers is pretty fast.

In general, std::sort() will be faster than anything you can write, unless you are a real sorting guru.

讨论(0)
• Here is an implementation of an odd-even merge sort network in C99 (sorry for the "wrong" language):

``````#define CMP_SWAP(i, j) if (a[i] > a[j])              \
{ int tmp = a[i]; a[i] = a[j]; a[j] = tmp; }

void sort8_network(int *a)
{
CMP_SWAP(0, 1); CMP_SWAP(2, 3); CMP_SWAP(4, 5); CMP_SWAP(6, 7);
CMP_SWAP(0, 2); CMP_SWAP(1, 3); CMP_SWAP(4, 6); CMP_SWAP(5, 7);
CMP_SWAP(1, 2); CMP_SWAP(5, 6); CMP_SWAP(0, 4); CMP_SWAP(1, 5);
CMP_SWAP(2, 6); CMP_SWAP(3, 7); CMP_SWAP(2, 4); CMP_SWAP(3, 5);
CMP_SWAP(1, 2); CMP_SWAP(3, 4); CMP_SWAP(5, 6);
}
``````

I timed it on my machine against insertion sort

``````void sort8_insertion(int *a)
{
for (int i = 1; i < 8; i++)
{
int tmp = a[i];
int j = i;
for (; j && tmp < a[j - 1]; --j)
a[j] = a[j - 1];
a[j] = tmp;
}
}
``````

For about 10 million sorts (exactly 250 times all the 40320 possible permutations), the sort network took 0.39 seconds while insertion sort took 0.88 seconds. Seems to me both are fast enough. (The figures inlcude about 0.04 seconds for generating the permutations.)

讨论(0)
• For very small numbers of ints, bubble sort can be very fast. Bubble sort with numerical comparisons can be written with a very low overhead and for small n, the actual speed differences between O(n log n) and O(n^2) washes out.

讨论(0) 看不清?