Given $n$ dots on a plane, such as: n couples ($x_i$,$y_i$)
I would like to find a line parallel to y-axis ( $x=b$ ), such that the sum of all of the point's distances from that line will be minimal
In order to do that, I need to write an alogrithem with a linear run-time ( $O(n)$ )
MY METHOD
I relate only for the $x$ values of each point as an element in an Array called $A$
So I used theSelect(A,left, right, p)
&Partition
Alogorithm in order to find the median of medians of the array
pseudo-code:
Select(A,left,right,p)
n<-right-left+1
if n=1 then
return A[left]
m<-⌈n/5⌉
let B array with length m
for i <-1 to m
B[i]=medianOf5(A,left,right,x)
x<-Select(B,1,m, ⌈m/2⌉)
q <- partition(A,left,right,x)
k <-q-left+1
if p<k then
return Select(A,left,q,p)
if p>k
return Select(A,q,right,p)
But, using median of medians seems unnecessary
If so, is there an easier way to that? if not ( or if yes for that matter ) was my way correct?