I want to describe an algorithm with time complexity $O(m)$ that, given a set $M$ with $m$ numbers and a positive integer $p \leq m$, returns the $p$ closest numbers to the median element of the set $M$.
low=m/2-(p-1)
high=(m-1)/2+(p-1)
Algorithm(A,begin,end,low,high){
if (begin<end){
k=medianofMedians(A,low,high)
q=hoare(A,start,begin,end,k)
if (q>low) Algorithm(A,begin,q-1,low,high)
if (q<high) Algorithm(A,q+1,end,low,high)
}
}
Is it right? Now we have an interval, to which the $p$ closest elements belong, but this interval contains $2p$ elements. How could we find the $p$ closest numbers to the median element of the set $M$?
EDIT:
function medianOfMedians(list, left, right)
numMedians = ceil((right - left) / 5)
for i from 0 to numMedians
// get the median of the five-element subgroup
subLeft := left + i*5
subRight := subLeft + 4
if (subRight > right) subRight := right
medianIdx := selectIdx(list, subLeft, subRight, (subRight - subLeft) / 2)
swap list[left+i] and list[medianIdx]
return selectIdx(list, left, left + numMedians - 1, numMedians / 2)
hoare(arr,start,end, pivot)
i,j = start,end
while i < j:
while i < j and arr[i] <= pivot:
i += 1
while j >= i and arr[j] > pivot:
j -= 1
if i < j:
arr[i],arr[j] = arr[j],arr[i]
return j
EDIT 2:
Taking into consideration this algorithm:
Select-k(S, k)
if n = k then return S
else i ← k mod 2
low ←Select(A, 1, n, b n/2c − b k/2c)
high ←Select(A, 1, n, b n/2c + b k/2c)
if i = 0 then if low ≤ high
then high ←Select(A, 1, n, b n/2c+b k/2c−1)
else low ←Select(A, 1, n, b n/2c − b k/2c + 1)
for i ← 1 to n do
if low ≤ S[i] ≤ high then return S[i]
how can we know that we return the right $p$ nearesrt elements to the median element?