Lec04a-full version
Lec04a-full version
(S1 AY2024/25)
Lecture 4a: Lower Bound for Comparison-Based Sorting
Sorting
• Input: an array 𝐴𝐴 = (𝑎𝑎1 , 𝑎𝑎2 , … , 𝑎𝑎𝑛𝑛 ) of elements. Examples:
• Insertion sort
• Goal: Sort the elements in 𝐴𝐴 in non-decreasing order. • Selection sort
• A permutation (𝑎𝑎1′ , 𝑎𝑎2′ , … , 𝑎𝑎𝑛𝑛′ ) of 𝐴𝐴 such that 𝑎𝑎1′ ≤ 𝑎𝑎2′ ≤ ⋯ ≤ 𝑎𝑎𝑛𝑛′ . • Merge sort
• Heap sort
• Quick sort
Sorting
Worst-case time complexity
• A permutation (𝑎𝑎1′ , 𝑎𝑎2′ , … , 𝑎𝑎𝑛𝑛′ ) of 𝐴𝐴 such that 𝑎𝑎1′ ≤ 𝑎𝑎2′ ≤ ⋯ ≤ 𝑎𝑎𝑛𝑛′ . • Merge sort 𝑂𝑂 𝑛𝑛 log 𝑛𝑛
• Heap sort 𝑂𝑂 𝑛𝑛 log 𝑛𝑛
• Quick sort 𝑂𝑂 𝑛𝑛2
• A permutation (𝑎𝑎1′ , 𝑎𝑎2′ , … , 𝑎𝑎𝑛𝑛′ ) of 𝐴𝐴 such that 𝑎𝑎1′ ≤ 𝑎𝑎2′ ≤ ⋯ ≤ 𝑎𝑎𝑛𝑛′ . • Merge sort 𝑂𝑂 𝑛𝑛 log 𝑛𝑛
• Heap sort 𝑂𝑂 𝑛𝑛 log 𝑛𝑛
• Quick sort 𝑂𝑂 𝑛𝑛2
• A permutation (𝑎𝑎1′ , 𝑎𝑎2′ , … , 𝑎𝑎𝑛𝑛′ ) of 𝐴𝐴 such that 𝑎𝑎1′ ≤ 𝑎𝑎2′ ≤ ⋯ ≤ 𝑎𝑎𝑛𝑛′ . • Merge sort 𝑂𝑂 𝑛𝑛 log 𝑛𝑛
• Heap sort 𝑂𝑂 𝑛𝑛 log 𝑛𝑛
• Quick sort 𝑂𝑂 𝑛𝑛2
𝐈𝐈𝐈𝐈 (𝐴𝐴 𝑖𝑖 < 𝐴𝐴 𝑗𝑗 ), 𝐭𝐭𝐭𝐭𝐭𝐭𝐭𝐭 Do some work 𝐈𝐈𝐈𝐈 (𝐴𝐴 𝑖𝑖 + 𝐴𝐴 𝑗𝑗 = 𝐴𝐴 𝑘𝑘 ), 𝐭𝐭𝐭𝐭𝐭𝐭𝐭𝐭 Do some work
𝐈𝐈𝐈𝐈 (𝐴𝐴 𝑖𝑖 = 𝐴𝐴 𝑗𝑗 ), 𝐭𝐭𝐭𝐭𝐭𝐭𝐭𝐭 Do some work 𝐈𝐈𝐈𝐈 (𝐴𝐴 𝑖𝑖 = 𝑘𝑘), 𝐭𝐭𝐭𝐭𝐭𝐭𝐭𝐭 Do some work
𝐈𝐈𝐈𝐈 (𝐴𝐴 𝑖𝑖 is odd), 𝐭𝐭𝐭𝐭𝐭𝐭𝐭𝐭 Do some work
𝐈𝐈𝐈𝐈 (the 𝑗𝑗th bit of 𝐴𝐴 𝑖𝑖 is 1), 𝐭𝐭𝐭𝐭𝐭𝐭𝐭𝐭 Do some work
Comparison-based sorting All of them are comparison-based.
High pay?
yes no
yes no yes no
Don’t take the job Take the job Take the job Don’t take the job
Decision trees
• A decision tree is a rooted tree.
• Start from the root.
• At every vertex, a question is asked.
• Depending on the answer, a child is chosen.
• At a leaf, a decision is taken.
Decision trees
• A decision tree is a rooted tree.
• Start from the root.
• At every vertex, a question is asked.
• Depending on the answer, a child is chosen.
• At a leaf, a decision is taken.
• Any comparison-based algorithm can be modeled using a decision tree:
• A comparison ⟷ A question asked at a node.
• Program state depends on the result of the comparison ⟷ Chosen child depends on the
answer to the question.
• Output of the algorithm ⟷ Decision at a leaf.
𝑎𝑎1 ≤ 𝑎𝑎2 ?
yes no
yes no yes no
𝑎𝑎1 , 𝑎𝑎2 , 𝑎𝑎3 𝑎𝑎1 , 𝑎𝑎3 , 𝑎𝑎2 𝑎𝑎3 , 𝑎𝑎1 , 𝑎𝑎2 𝑎𝑎2 , 𝑎𝑎1 , 𝑎𝑎3 𝑎𝑎2 , 𝑎𝑎3 , 𝑎𝑎1 𝑎𝑎3 , 𝑎𝑎2 , 𝑎𝑎1
An example
•Worst-case
A comparison-based
running time ≥algorithm for sorting
worst-case number 𝐴𝐴 = 𝑎𝑎1 , 𝑎𝑎
of comparisons , 𝑎𝑎3 . of the tree
=2height
𝑎𝑎1 ≤ 𝑎𝑎2 ?
yes no
yes no yes no
𝑎𝑎1 , 𝑎𝑎2 , 𝑎𝑎3 𝑎𝑎1 , 𝑎𝑎3 , 𝑎𝑎2 𝑎𝑎3 , 𝑎𝑎1 , 𝑎𝑎2 𝑎𝑎2 , 𝑎𝑎1 , 𝑎𝑎3 𝑎𝑎2 , 𝑎𝑎3 , 𝑎𝑎1 𝑎𝑎3 , 𝑎𝑎2 , 𝑎𝑎1
Proof of the theorem
Theorem: The worst-case time complexity of any
comparison-based sorting algorithm is Ω 𝑛𝑛 log 𝑛𝑛 .
Proof:
• Model the algorithm as a decision tree, which is a binary tree with at least 𝑛𝑛! leaves:
• Each permutation is a possible answer.
• The height of the binary tree is at least log 𝑛𝑛! .
Proof of the theorem
Theorem: The worst-case time complexity of any
comparison-based sorting algorithm is Ω 𝑛𝑛 log 𝑛𝑛 .
Proof:
• Model the algorithm as a decision tree, which is a binary tree with at least 𝑛𝑛! leaves:
• Each permutation is a possible answer.
• The height of the binary tree is at least log 𝑛𝑛! .
Stirling's approximation
https://en.wikipedia.org/wiki/Stirling%27s_approximation
Question 1 @ VisuAlgo online quiz
• Is the following claim true or false?
False:
• There are 5! = 120 permutations of 5 elements.
• In any binary tree of height at most 6, the number of leaves is at most 26 = 64.
• 120 > 64, so 6 comparisons are not enough.
Question 2 @ VisuAlgo online quiz
Input: 𝑘𝑘 sorted arrays 𝐴𝐴1 1. . 𝑛𝑛 , 𝐴𝐴2 1. . 𝑛𝑛 , … , 𝐴𝐴𝑘𝑘 1. . 𝑛𝑛 .
Goal: Merge the 𝑘𝑘 sorted arrays into one sorted array of length 𝑘𝑘𝑘𝑘.
Question: What is a tight lower bound of the worst-case running time
for comparison-based algorithms for this task?
• Ω 𝑘𝑘𝑘𝑘
2 8 16 21 7 11 13 19
• Ω 𝑘𝑘𝑘𝑘 log 𝑘𝑘
• Ω 𝑘𝑘𝑘𝑘 log 𝑛𝑛
2 2 7 8 11 13 16 19 21
• Ω 𝑘𝑘 𝑛𝑛
Question 2 @ VisuAlgo online quiz
Input: 𝑘𝑘 sorted arrays 𝐴𝐴1 1. . 𝑛𝑛 , 𝐴𝐴2 1. . 𝑛𝑛 , … , 𝐴𝐴𝑘𝑘 1. . 𝑛𝑛 .
Goal: Merge the 𝑘𝑘 sorted arrays into one sorted array of length 𝑘𝑘𝑛𝑛.
Question: What is a tight lower bound of the worst-case running time
for comparison-based algorithms for this task?
• Ω 𝑘𝑘𝑛𝑛
2 8 16 21 7 11 13 19
• Ω 𝑘𝑘𝑘𝑘 log 𝑘𝑘
• Ω 𝑘𝑘𝑘𝑘 log 𝑛𝑛
2 2 7 8 11 13 16 19 21
• Ω 𝑘𝑘 𝑛𝑛
Answer
• Ω 𝑘𝑘𝑘𝑘 log 𝑘𝑘 is a tight lower bound.
(𝑘𝑘𝑘𝑘)!
log = log 𝑘𝑘𝑛𝑛 ! − 𝑘𝑘 log 𝑛𝑛!
𝑛𝑛! 𝑘𝑘
∈ 𝑘𝑘𝑛𝑛 log 𝑘𝑘𝑘𝑘 − 𝑘𝑘𝑛𝑛 log 𝑒𝑒 + 𝑂𝑂 log 𝑘𝑘𝑘𝑘 − 𝑘𝑘 𝑛𝑛 log 𝑛𝑛 − 𝑛𝑛 log 𝑒𝑒 + 𝑂𝑂 log 𝑛𝑛
= 𝑘𝑘𝑛𝑛 log 𝑘𝑘 ± 𝑂𝑂 log 𝑛𝑛 ⊆ Θ 𝑘𝑘𝑘𝑘 log 𝑘𝑘
Answer
• Ω 𝑘𝑘𝑘𝑘 log 𝑘𝑘 is a tight lower bound.
(𝑘𝑘𝑘𝑘)!
log = log 𝑘𝑘𝑛𝑛 ! − 𝑘𝑘 log 𝑛𝑛!
𝑛𝑛! 𝑘𝑘
∈ 𝑘𝑘𝑛𝑛 log 𝑘𝑘𝑘𝑘 − 𝑘𝑘𝑛𝑛 log 𝑒𝑒 + 𝑂𝑂 log 𝑘𝑘𝑘𝑘 − 𝑘𝑘 𝑛𝑛 log 𝑛𝑛 − 𝑛𝑛 log 𝑒𝑒 + 𝑂𝑂 log 𝑛𝑛
= 𝑘𝑘𝑛𝑛 log 𝑘𝑘 + 𝑂𝑂 log 𝑘𝑘𝑛𝑛 − 𝑂𝑂 log 𝑛𝑛 ⊆ Θ 𝑘𝑘𝑘𝑘 log 𝑘𝑘
𝐂𝐂𝐂𝐂𝐂𝐂𝐂𝐂𝐂𝐂𝐂𝐂𝐂𝐂𝐂𝐂𝐂𝐂𝐂𝐂𝐂𝐂𝐂𝐂(𝐴𝐴)
• For all 𝑖𝑖 ∈ 1,2, … , 𝑘𝑘 , compute 𝐜𝐜𝐜𝐜𝐜𝐜𝐜𝐜𝐭𝐭 𝑖𝑖 = the number of appearances of 𝑖𝑖 in 𝐴𝐴.
• Set the initial 𝐜𝐜𝐜𝐜𝐜𝐜𝐜𝐜𝐭𝐭1 entries of 𝐴𝐴 to be 1.
• Set the next 𝐜𝐜𝐜𝐜𝐜𝐜𝐜𝐜𝐭𝐭 2 entries of 𝐴𝐴 to be 2.
• Set the next 𝐜𝐜𝐜𝐜𝐜𝐜𝐜𝐜𝐭𝐭 3 entries of 𝐴𝐴 to be 3.
• …