@zqbinggong
2018-03-10T21:47:56.000000Z
字数 2192
阅读 1241
选择算法
顺序统计量
算法导论
Randomized-Select(A,p,r,i)
if p == r
return A[p]
q = Randomized-Partiton(A,p,r)
k = q - p + 1
if i == k
return A[q]
else if i < k
return Randomized-Partition(A,p,q-1,i)
esle return Randomized-Partition(A,q+1,r,i-k)
--
Two-Array-Median(X,Y)
n = X.length
median = Find-Median(X,Y,n,1,n)
if median == not_found
median = Find-Median(Y,X,n,1,n)
return median
Find-Median(X,Y,n,low,high)
if low > high
return not_found
else k = (low+high)/2 #向下取整
if k == n and A[n] <= B[1]
return A[n]
else if k < n and B[n-k] <= A[k] <= B[n-k+1]
return A[k]
else if A[k] > B[n-k+1]
return Find-Median(X,Y,n,low,k-1)
else return Find-Median(X,Y,n,k+1,high)
Weighted-Median(X)
if n == 1
return x1
else if n == 2
if w1 >= w2
return x1
else return x2
else find the median xk of X
partition X around xk
WL = the sum of weights whose element is less than xk
WG = the sum of weights whose element is greater than xk
if WL < 1/2 and WG < 1/2
return xk
else if WL > 1/2
wk = wk + WG
X' = {x belong to X where x <= xk}
return Weighted-Median(X')
ekse wk = wk + WL
X' = {x belong to X where x >= xk}
return Weighted-Median(X')