I was reading the article on finding the k-th highest element in an array through the median-of-medians algorithm at ardendertat. In the portion explaining the complexity, the author seems to have discounted one factor, the cost of finding the median-of-medians for each partition recursively. Surely I can’t partition all sub-arrays by the initial pivot, right? So won’t this add to the complexity?
I was reading the article on finding the k-th highest element in an array
Share
The position
pof the median of medians after partitioning the array is between0.3*nand0.7*n.So after one round, we have three possibilities:
p == n-k, by pure luck, the first pivot is thek-th largest element, found in O(n) (median of medians and partitioning are both O(n)).p > n-k, then thek-th largest element is smaller than the first pivot, and we need to find thek - (n-p)-th largest element of the first part, which has at most0.7*nelements, so the total cost for finding thek-th largest element isp < n-k, then we need to find thek-th largest element of the second part (after the pivot), that part is also at most0.7*nelements large, so we again have the estimateIterating, we find
which evidently is O(n).