(W3) Divide and Conquer Flashcards
(3 cards)
How to find the k closest elements to the median in an unordered sequence - within O(N)
use QuickSelect to find the median
For the remaining numbers, convert them to the absolute difference to the median
then find the k-smallest number, and all numbers between 0 and k-smallest number are your actual values
Can you use the average as the pivot for QuickSort? Give a counter example
No - there are situations where this will be horribly inefficient
Take the sequence n! - the average (n!/n), will be (n-1)! - therefore only a single value will be larger than the average (and therefore the pivot). Hence, this will result in O(n^2) runtime
How to think of QuickSelect in terms of finding values?
It’s the percentile. If you want the 60th percentile then you want the 6/10-th smallest element