(W3) Divide and Conquer Flashcards

(3 cards)

1
Q

How to find the k closest elements to the median in an unordered sequence - within O(N)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can you use the average as the pivot for QuickSort? Give a counter example

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to think of QuickSelect in terms of finding values?

A

It’s the percentile. If you want the 60th percentile then you want the 6/10-th smallest element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly