Exercise 9.3.7
Describe an
O(n) -time algorithm that, given a setS ofn distinct numbers and a positive integerk≤n , determines thek numbers inS that are closest to the median ofS .
This is a fun exercise.
- We find the median of the array in linear time
- We find the distance of each other element to the median in linear time
- We find the
k -th order statistic of the distance, again, in linear time - We select only the elements that have distance lower than or equal to the
k -th order statistic
Read full article from Exercise 9.3.7
No comments:
Post a Comment