For the general case , where the range of elements is unlimited, such a data structure does not exist on the basis of any algorithm based on comparison, since this will make it possible to sort O(n) .
Evidence. Suppose such DS exist, let it be D
Let A be the input array to sort. (Suppose A.size() even for simplicity, which can be easily relaxed by adding a garbage item and discarding it later).
sort(A): ds = new D() for each x in A: ds.add(x) m1 = min(A) - 1 m2 = max(A) + 1 for (i=0; i < A.size(); i++): ds.add(m1)
Claim 1: This algorithm works in O(n) . Evidence. Since we have constant operations add () and median (), each of them is O(1) per iteration, and the number of iterations is linear - the complexity is linear.
Claim 2: output sorted (A).
Proof (recommendations): after inserting n times m1 median is the smallest element in A Every two inserts after he advances the median by one element, and since the advances are sorted, the overall output is sorted.
Since the indicated algorithm is sorted in O(n) and is not possible in the comparison model, such DS do not exist.
QED
amit
source share