Algorithm 1 FindMedian(L)
Input: L as input list containing n real numbers
Output: The median of L as m
if n ≤ 10 then
Sort L and return the median m
end if
Divide L into n/5 lists of size 5 each
(Making up the corner case that n is not at multiple of 5 is skipped. An easy way is to add minimum and maximum of the array at the head and the tail)
Sort each list, let ith list be ai ≤ a'i ≤ bi ≤ ci ≤ c'i, i = 1, 2, ..., n/5
Recursively find median of b1, b2, ..., bn/5, call it b*
Reorder indices so that b1, b2, ..., bn/10 ≤ b* < bn/10+1, ..., bn/5
if n/5 is odd ⟹ b* = bn/10 then
A = {a1, a'1, b1, a2, a'2, b2, ..., an/10-1, a'n/10-1, bn/10-1, an/10, a'n/10},
C = {cn/10, c'n/10, bn/10+1, cn/10+1, c'n/10+1, bn/10+2, cn/10+2, c'n/10+2, ..., bn/5, cn/5, c'n/5}
else
A = {a1, a'1, b1, a2, a'2, b2, ..., an/10-1, a'n/10-1, bn/10-1, an/10, a'n/10},
C = {cn/10+1, c'n/10+1, bn/10+2, cn/10+2, c'n/10+2, ..., bn/5, cn/5, c'n/5}
end if
(To make sure both A and C have equal number of elements, approximately 3/10n, when n is large)
Drop A and C from the original list L, to get a new list L', with n - 3/10n - 3/10n = 2/5n elements
Find median of remaining L' recursively and return it as m.
(a) Let T(n) be the running time of this algorithm, find the recurrence formula, and then solve it.
(b) Is this algorithm correct? If yes, try to prove it; otherwise, find a counter case (something like the true median is in A or C and is dropped).