If you have one huge amount of numbers and one hundred computers, How would you find the median of the numbers?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Use selection algorithm.
- Split the array of number to 100 partitions.
- Each processor should use the general pivot to split the array to two groups (left/right)
- then each processor should send the size of those 2 groups to the leader
- the leader should calculate which group is smaller and broadcast a message to get rid from one of those groups.
- go back to step 2 until you find the median
this solution has an avg runtime of O(n) in order to make it asymptotic runtime of O(n), each processor should split the numbers to groups of 5 elements find the median of each group (using insertion sort) and send those medians back to the leader, the leader will choose the median of those medians (using the same algo) and that will be the pivot
read the wiki article - http://en.wikipedia.org/wiki/Selection_algorithm