Everything2
Near Matches
Ignore Exact
Full Text
Everything2

Sorting is O(n log n)

created by pfft

(idea) by pfft (2.2 hr) (print)   ?   (I like it!) 1 C! Sat Sep 16 2000 at 16:34:27

Theorem: Any comparison sort must in the worst case make at least Ω(n log n) comparisons to sort n keys.

Proof:
A sorting algorithm, viewed abstractly, takes a sequence of keys k1, k2, ... kn, and outputs the permutation that will sort them.

For a given n, we can represent the optimal algorithm as a binary decision tree: the internal nodes are comparisons of two keys, and at the leaves are the correct permutations. Sorting a particular sequence is equivalent to walking the tree from the root to a leaf. The number of comparisons in the worst case is the height of the tree.

Now, there are n! permutations of a given sequence, so the tree has n! leaves. Then its height must be at least ceil(log n!). But we know from Stirling's Formula that n! ≥ nne-n, which is equivalent to log(n!) ≥ n log n - n log e. So worst case ≥ ceil(log n!) ≥ n log n - n log e = &Omega(n log n). ∎

One way of thinking about this result is from an information theory viewpoint: there are n! possible permutations of the sequence, so the scrambled version can contain up to log(n!) bits of entropy. But by comparing two keys, we can extract at most one bit of information.

A counterexample for sorts that are not comparison-based: radix sort runs in O(n) time, if we restrict the keys to a certain interval.


(idea) by flyingroc (1.3 y) (print)   ?   (I like it!) Sat Sep 30 2000 at 20:13:36

Just a little nitpicking, the decision tree based proof is a lower bound proof, thus the proper notation above should be Ω(n log n). To prove that the *problem* of sorting is O(n log n), we can just look at one algortihm such as merge sort, and show that it is O(n log n) in the worst case.

Proving the lower bound is more involved, as shown in the writeup above. As a consequence of knowing that the problem of sorting is O(n log n) and Ω(n log n), we know that the problem of sorting is Θ(n log n).

Again, as above a caveat. The proof is only for comparison-based sorting. Bin sort (also known as Bucket Sort) is Θ(n), but you need to reserve space for all possible keys.


printable version
chaos

Radix Sort log(n!) Big-oh notation bin sort
Sorting Algorithms bucket sort theta Stirling's Formula
Zen Sort Sorting Algorithms : Rank Sort Omega Graham's scan
Merge Sort convex hull Ω radish sort
algorithm Altoids comparison sort QED
counterexample big-O notation information theory Capitol
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Things you could have written:
Multiple Sclerosis
Hugh Hefner
Luddite
War on Drugs
Only Slightly a Geek Girl
Osama bin Laden
How the Alphabet Began
Red Headed Stranger
The Origins of the First World War
Arthur C. Clarke
Childlessness
Croque monsieur
Basics of Defensive Football
New Writeups
anndandridge
Dorothy Dandridge(person)
PaulM
ignominity(idea)
Clarke
Multiculturalism(idea)
aneurin
Earl of Landaff(person)
Heitah
Pseudocide(idea)
XWiz
Google Knol(lede)
Mythi
July 24, 2008(personal)
locke baron
The fall of Earth(fiction)
BookReader
Fear the Cold(dream)
Pavlovna
Kathleen MacInnes(person)
stainedglass
1(fiction)
kalen
Three "T"s(idea)
octillion369
Undead(idea)
archiewood
Ico(fiction)
Heisenberg
Why I love Everything2(log)
This affordable entertainment brought to you by The Everything Development Company