How can anything beat n log n? iahmed.me · 16 points · 2 comments · HN thread TL...

@best

How can anything beat n log n? iahmed.me · 16 points · 2 comments · HN thread

TL;DR The post explains why sub-n log n results don't contradict the lower bound for sorting. The n log n bound holds for comparison-based sorting with black-box items and factorial-sized answer spaces; algorithms like Bucket and Radix Sort beat it by inspecting item values and widening the branching factor to scale with n. It frames the bound with entropy and gives a table of problems with their entropy floors.

  • The n log n bound applies to 2-way comparison steps on black-box items, where a sorting answer space has n! outcomes and entropy about n log2 n.
  • A k-way step yields log_k N steps of depth, so constant k only changes the cost by a constant factor, while k growing with n (e.g., k = n^alpha) removes the log n factor.
  • Bucket Sort and Radix Sort sort n numbers in O(n) by reading digits or values directly rather than comparing items.
  • Entropy gives a floor but not a guarantee: finding the maximum has log2 n bits of entropy yet needs n-1 comparisons because each step removes only one candidate.
  • Headlines about sub-n log n results usually involve problems whose answer space is not factorial-sized, such as shortest-path distances or integer multiplication with only 2^(2n) outputs.

Discussion The thread is short, with two top-level comments, and is mostly a list of surprising complexity results. One commenter adds examples: computing the median is Θ(n), and computing the Nth prime can be done in o(N^(3/4)). A reply notes that placing 100 already-known items into their sorted positions can be done in O(n) rather than n log n. The mood is curious and appreciative, with no disagreement.

  • Surprising linear-time results beat intuition about n log n (most): Commenters share problems whose complexity surprised them, such as linear-time median finding. The shared tone is appreciation for counterintuitive results.
  • Placing already-known items in sorted order is linear (a few): One commenter was surprised that inserting 100 known items in random order into their sorted slots is O(n) rather than n log n. This is an example of exploiting known values.

#algorithms #complexityTheory #sorting #informationTheory #entropy