ENع
→The n log n floor
الدرس 31 · Greedy and divide and conquer

Why comparison sorts can't beat n log n

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةAdvanced
عن هذا الدرس

Merge sort, heapsort and quicksort on average all land on n log n comparisons. That is no accident: any sort that learns about its items only by comparing them must use about that many. Watch a short Python sort for three items run on 5, 9, 2, draw every path it could take as a decision tree, then count leaves: n items have n! orders, a tree of height h has room for only 2ʰ, so h ≥ log₂(n!), which grows like n log n. The Software lessons on sorting (insertion sort, merge sort and Timsort) and recursion, the Maths lesson on proofs, and "Quicksort and quickselect: partitions and pivots" come first.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 31 · تفاعلي
01 / 10

A sort that only compares

A comparison sort learns about its items in only one way: it asks “is x ≤ y?” and gets yes or no. Insertion sort, merge sort, quicksort and Python's sorted work like this. Our input: a = 5, b = 9, c = 2.

comparison model: the only way to learn about the items
  ask  x <= y ?   ->  yes / no

input: a = 5, b = 9, c = 2
goal:  return the three values in sorted order
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. A sort that only compares

    A comparison sort learns about its items in only one way: it asks “is x ≤ y?” and gets yes or no. Insertion sort, merge sort, quicksort and Python's sorted work like this. Our input: a = 5, b = 9, c = 2.

    comparison model: the only way to learn about the items
      ask  x <= y ?   ->  yes / no
    
    input: a = 5, b = 9, c = 2
    goal:  return the three values in sorted order
  2. Three items as plain ifs

    This Python function sorts exactly three items. Each if asks one question. Each return is one answer: the three items in sorted order. It asks the same questions, in the same order, as insertion sort does on three items.

     1  def sort3(a, b, c):
     2    if a <= b:
     3      if b <= c: return a, b, c
     4      if a <= c: return a, c, b
     5      return c, a, b
     6    if a <= c: return b, a, c
     7    if b <= c: return b, c, a
     8    return c, b, a
    
    sort3(5, 9, 2) returns (2, 5, 9)
    lines 2, 3, 4, 6, 7 ask a question; every return is an answer
  3. Question one: is a ≤ b?

    Every run starts at the same top question, line 2: is a ≤ b? Here 5 ≤ 9, so the answer is yes and the run follows the yes branch. The input card hops onto the first question.

    line 2   a <= b ?
             5 <= 9   yes  -> line 3
    
    comparisons so far: 1
  4. Down the tree to a leaf

    Line 3 asks b ≤ c: 9 ≤ 2 is no. Line 4 asks a ≤ c: 5 ≤ 2 is no. Line 5 is a leaf: return c, a, b, which is 2, 5, 9. Three questions sorted this input.

    line 2   a <= b ?   5 <= 9   yes
    line 3   b <= c ?   9 <= 2   no
    line 4   a <= c ?   5 <= 2   no
    line 5   return c, a, b  ->  (2, 5, 9)
    
    comparisons: 3
  5. Every path is a tree

    Each input follows one path like this. Draw all the paths at once and you get a decision tree: 5 questions and 6 leaves. Its height, the longest path from the top to a leaf, is the worst-case number of comparisons: 3.

    leaf        depth   (comparisons)
      a b c       2
      a c b       3
      c a b       3
      b a c       2
      b c a       3
      c b a       3
    
    height = longest path = 3
  6. Six orders need six leaves

    Three different items can arrive in 3! = 3 × 2 × 1 = 6 orders. Each order needs its own leaf. If two orders reached the same leaf, the sort would rearrange both the same way, and one would come out wrong.

    the 6 orders of three distinct items, and the leaf each one reaches
      a=1 b=2 c=3  ->  leaf a b c  ->  1 2 3
      a=1 b=3 c=2  ->  leaf a c b  ->  1 2 3
      a=2 b=1 c=3  ->  leaf b a c  ->  1 2 3
      a=2 b=3 c=1  ->  leaf c a b  ->  1 2 3
      a=3 b=1 c=2  ->  leaf b c a  ->  1 2 3
      a=3 b=2 c=1  ->  leaf c b a  ->  1 2 3
    
    n! = n × (n - 1) × ... × 1 orders, all different leaves
  7. Height 2 is too short

    Each question splits a path in two, so a tree of height h has at most 2ʰ leaves. Height 2 gives at most 4, fewer than 6. So every comparison sort needs 3 questions for some order of three items.

      h    2^h  >= 6?
      0      1     no
      1      2     no
      2      4     no
      3      8    yes
    
    smallest h with 2^h >= 3! = 6 is h = 3
    so some path must ask a third question
  8. Any n: at least log₂(n!)

    The same count works for any n. n items have n! orders, so the tree needs 2ʰ ≥ n!, which means h ≥ log₂(n!). Four items need at least 5 comparisons; ten items need at least 22.

      n       n!    min h
      3        6        3
      4       24        5
      5      120        7
     10  3628800       22
    
    min h = smallest h with 2^h >= n!  =  ⌈log₂(n!)⌉
  9. Why log₂(n!) is n log n

    At least n/2 of the factors in n! are n/2 or more, so log₂(n!) ≥ (n/2)·log₂(n/2), which grows like n log n. So every comparison sort takes Ω(n log n) comparisons in the worst case (Ω: at least, up to a constant).

    n! = 1 × 2 × ... × n  >=  (n/2)^(n/2)
      n  n/2·lg n/2       lg n!      n lg n
      4         2.0         4.6         8.0
     10        11.6        21.8        33.2
    100       282.2       524.8       664.4
    1000      4482.9      8529.4      9965.8
    
    lg n! is squeezed between (n/2)·lg(n/2) and n·lg n: Θ(n log n)
  10. Who meets it, who escapes

    Merge sort and heapsort use O(n log n) comparisons, so they match this floor: no comparison sort beats them by more than a constant factor. Counting sort and radix sort escape it because they never compare two items; they use values as positions.

    worst-case comparisons
      any comparison sort    Ω(n log n)   (this lesson)
      merge sort, heapsort   O(n log n)   optimal
      insertion sort         Θ(n²)
      counting / radix sort  no comparisons: the bound does not apply

اختبار يتاح بعد الخطوة الأخيرة.