General and practical sorting algorithm faster than O (n log n)?

Is there any practical algorithm for generic elements (as opposed to sorting sorting or sorting in bytes) that works faster than O (n log n)?

+6
sorting algorithm computer-science big-o
source share
4 answers

Many people mentioned theoretical information and Omega; (n lg n) tied to comparison sorting algorithms that cannot be broken down into sortings. ( This earlier question explores why this case.)

Nevertheless, there are some types of sorting, which, although they do not violate O (n lg n) in the average case, can be demonstrated to work faster on inputs that are already preliminarily brought to some extent. For example, Dijkstra anti-aliasing is performed in O (n) on already sorted inputs with the worst-case behavior of O (n lg n). One of my favorite varieties, the Cartesian tree species , provably optimally uses presortedness in several ways. For example, it can sort any sequence with a constant number of increasing or decreasing subsequences in time O (n), gracefully decreasing to O (n lg n) in the worst case.

For non-comparison varieties, there are some well-known but sophisticated sorting algorithms for integers that are superior to O (n lg n), making ingenious cunning manipulations. The best-known integer sorting algorithm is a randomized algorithm that can sort by O (n l log lg n), and the fastest deterministic algorithm for integer sorting runs in O (n log lg n) time. You may have heard that radix sorting works in O (n), although technically it is O (n lg U), where U is the largest value in array sorting.

In short, no, you cannot do much better than O (n lg n), but you can do a little better if you know something about your input.

+21
source share

For common elements that you can compare without accessing the internals, it is not possible to have a sorting algorithm faster than Theta (n log n). This is because there are n! (n factorial) of possible element orders, and you need Theta comparisons (n ​​log n) to distinguish them all.

+3
source share

How many items? Even if it's something like N 1.2, Shell-Metzner sorting is often faster than most of the others, up to several thousand items (or so).

It also depends on what you mean by "generic" and "practical." Radial sorting can beat O (n log n), and it works for a fairly wide range of data (but definitely not for everyone).

If your idea of ​​the practical and general limitations of the algorithm to one that directly compares the elements, then no - nothing (or can never) be better than O (n log n). This has been proven for a long time.

+3
source share

Not. This is one of the few strict minimum boundaries for the algorithms that we have. For a set of n elements, there is n! different orders, so to indicate this order, we need the log (n!) bits. According to Stirling's approximation, this is approximately n log n. For each comparison between elements, we get essentially one bit of information (ignoring the possibility of equal elements).

+1
source share