Lecture 1 - Comparison Sorts Flashcards
What comparison sorts have average+worst time complexity of O(n^2)?
Selection sort
Insertion sort
Bubble sort
What comparison sorts have average and worst time complexity of O(nlogn)?`
Merge sort
Heap sort
What is the fastest comparison - based sorting algorithm in practice?
Quicksort
What is the worst time complexity of quicksort?
O(n^2)
What is the average time complexity of quicksort?
O(nlogn)
What is the claim on comparison based sorting algorithms?
No comparison based sorting algorithm can have a better complexity than O(nlogn).
How to prove comparison based algorithms can’t do better than O(nlogn)?
Via a decision tree
What does the number of leafs of a decision tree represent?
Number of outcomes.
This can also be calculated via !n.
Why can the complexity not be better than the height of the decision tree?
Since we have to reach a leaf to get a result.
What is the maximum number of leaf nodes in a binary tree?
2^(h+1) - 1
Is a decision tree a binary tree?
Yes
What is the equation to prove the O(nlogn) claim?
!n <= 2^(h+1)-1 < 2^(h+1)
How to solve !n <= 2^(h+1)-1 < 2^(h+1).
- take log2 of both sides
h+1 > log2 (!n) - since !n > (n/2) ^ (n/2)
h+1 > log2(n/2)^(n/2) - simplify
h+1 > n/2 log2(n/2) - expand
h+1 > n/2 log2n - n/2 log22
h > n/2 log2n - n/2 - 1
-> this gives O(nlogn) ignoring smaller terms