Comment Comparison sort cannot be way better than N*log(N) (Score 1) 98
It can be proven that sorting by comparison will take in the worst case scenario around N*log(N) comparisons.
The actual lower limit is log2(N!) - like in sorting by insertion. However, this is close to N*log(N) for big N.
One thing that you can optimize is the cache access, that can really make a huge time difference (like 10*X) at the same number of operations.
Another thing is to optimize common cases. Often you need to sort arrays that contain many parts that are already monotonic. The sort used in PHP will be very efficient for such cases.
A curiosity: you can sort 32bits integers in O(N) operations, adding each number to a binary tree with 32 levels, based on its bits: 0=left, 1=right. In the leafs, you just count the occurrences. Then walk the tree from left to right.
This seems to contradict the above. The catch is that there are 2^32 distinct integers and log2(N) = log2(2^32) = 32. So, for integers, log(N) is really a small constant that can be ignored. You can sort more than 2^32 integers with the same complexity, because there will be duplicates but the tree will have at most 2^32 leafs. However, the memory access pattern and additional memory might make this algorithm less appealing in practice.
And a half joke: sorting is computationally harder than chess: https://meaningofstuff.blogspo...