Medians don't merge at all. The exact answer needs ranks, and a server can report a rank without sending data: ask every server how many of its orders are at or below some value v, and add the answers. If at least half of all orders are at or below v, the median is at most v, so keep the low half of the range, v included. If not, the median is above v, so keep the values from v + 1 up. Each round halves the range of values the median could be. Hübschle-Schneider, Sanders and Müller describe the same idea for many machines: a selection algorithm, FR-select, adapted to a distributed memory parallel setting, where each round asks the nodes to count.
class Server:
"""Holds its orders and answers one kind of question: how many are at or below v."""
def __init__(self, values):
self._values = np.sort(values)
self.questions = 0
def count_leq(self, v):
self.questions += 1
return int(np.searchsorted(self._values, v, side="right"))
fleet = [Server(x) for x in nodes]
lo, hi = min(x.min() for x in nodes), max(x.max() for x in nodes)
median = lower_median(lambda v: sum(s.count_leq(v) for s in fleet), counts, lo, hi)
print(f"orders run from {lo} to {hi}")
print(f"median of the 10 medians: {np.median([np.median(x) for x in nodes])}")
print(f"binary search: {median} after {fleet[0].questions} rounds and {sum(s.questions for s in fleet)} questions")
print(f"sorted all at once: {np.sort(np.concatenate(nodes))[(counts - 1) // 2]}")