Median algorithm in O (log n)

How to remove the median of a set with time complexity O (log n)? Some idea?

+3
source share
9 answers

If the sort is sorted, the search for median information requires the extraction of O (1) elements. If the elements are in random order, it will not be possible to determine the median with confidence without studying most of the items. If you have considered most, but not all, of the elements that will ensure that the median is within a certain range [if the list contains duplicates, the upper and lower boundaries may coincide], but studying most of the elements in the list means extracting O (n) elements .

, , , O (1) O (n), .

+18

O (n) , , , . , , O (n).

- ?

+5

, , O (n). O (1) O (n log n), O (1), O (n logn n) . , , , O (n).

- , O (n) , , .

+4

Java, TreeSet:

public class SetWithMedian {
    private SortedSet<Integer> s = new TreeSet<Integer>();
    private Integer m = null;

    public boolean contains(int e) {
        return s.contains(e);
    }
    public Integer getMedian() {
        return m;
    }
    public void add(int e) {
        s.add(e);
        updateMedian();
    }
    public void remove(int e) {
        s.remove(e);
        updateMedian();
    }
    private void updateMedian() {
        if (s.size() == 0) {
            m = null;
        } else if (s.size() == 1) {
            m = s.first();
        } else {
            SortedSet<Integer> h = s.headSet(m);
            SortedSet<Integer> t = s.tailSet(m + 1);
            int x = 1 - s.size() % 2;
            if (h.size() < t.size() + x)
                m = t.first();
            else if (h.size() > t.size() + x)
                m = h.last();
        }
    }
}

(.. s.remove(s.getMedian()) ") O (log n).

. , :

private boolean isGood() {
    if (s.isEmpty()) {
        return m == null;
    } else {
        return s.contains(m) && s.headSet(m).size() + s.size() % 2 == s.tailSet(m).size();
    }
}

:

  • "s" , "m" .
  • "s" , "m".
  • x - , "m", y , "m". , , x ; x + 1 y.
+4

Red-black-tree. ur log (n). log (n), log (n).

+4

O (n) .

:

: n A [1... n] [ n ]

: n/2- .

(A [1..n], k = n/2):

- p 1... n

2 :

L - <= A [p]

R - > A [p]

if (n/2 == | L |) A [| L | + 1]

(n/2 < | L |) (L, k)

else (R, k - (| L | + 1)

:  O (n)   - . . , .

+2

, , n, , n ( log n) n , Quicksort. .

"" ( RNG), , (, ) .

+2

, , . , , , , , O (n). ( ) BFPRT O (n). : http://en.wikipedia.org/wiki/Selection_algorithm#Linear_general_selection_algorithm_-_Median_of_Medians_algorithm

, , , O (n), . - , "" . , 5 , . , , .

, . , , . : http://www.umiacs.umd.edu/research/EXPAR/papers/3494/node18.html

Note that you can find asymptotically faster algorithms there, however they are not practical enough for daily use. Best of all is the already mentioned sequential median median algorithm.

+2
source

To expand on rwong answer: Here is a sample code

// partial_sort example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;


int main () {
  int myints[] = {9,8,7,6,5,4,3,2,1};
  vector<int> myvector (myints, myints+9);
  vector<int>::iterator it;

  partial_sort (myvector.begin(), myvector.begin()+5, myvector.end());

  // print out content:
  cout << "myvector contains:";
  for (it=myvector.begin(); it!=myvector.end(); ++it)
    cout << " " << *it;

  cout << endl;

  return 0;
}

Conclusion: myvector contains: 1 2 3 4 5 9 8 7 6

The element in the middle will be median.

0
source

Source: https://habr.com/ru/post/1762981/


All Articles