Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ndsvw/median-maintenance

Median-Maintenance keeps track of the median of a growing list of numbers. O(log(n)) complexity for each of the n list elements => O(n*log(n))
https://github.com/ndsvw/median-maintenance

algorithm complexity logarithmic median

Last synced: 3 days ago
JSON representation

Median-Maintenance keeps track of the median of a growing list of numbers. O(log(n)) complexity for each of the n list elements => O(n*log(n))

Awesome Lists containing this project

README

        

## Median-Maintenance

Median-Maintenance keeps track of the median of a growing list of numbers.

### Complexity

* add(int x) => O(log(n)), n is the number of elements so far
* getMedian() => O(1)
* adding n elements to this data structure and return the median after each add-Operation: O(n*log(n))

### Examples

```java
MedianMaintenance mm = new MedianMaintenance(3);
mm.add(4);
mm.getMedian(); // returns 4
mm.add(10);
mm.getMedian(); // returns 4
mm.add(20);
mm.getMedian(); // returns 10
```

```java
MedianMaintenance mm = new MedianMaintenance(6);
mm.add(0);
mm.getMedian(); // returns 0
mm.add(1);
mm.getMedian(); // returns 0
mm.add(2);
mm.getMedian(); // returns 1
mm.add(3);
mm.getMedian(); // returns 1
mm.add(4);
mm.getMedian(); // returns 2
mm.add(5);
mm.getMedian(); // returns 2
```

```java
MedianMaintenance mm = new MedianMaintenance(6);
mm.addAndReturnMedian(4); // returns 4
mm.addAndReturnMedian(7); // returns 4
mm.addAndReturnMedian(1); // returns 4
mm.addAndReturnMedian(3); // returns 3
mm.addAndReturnMedian(10); // returns 4
mm.addAndReturnMedian(12); // returns 4
```

```java
MedianMaintenance mm = new MedianMaintenance();
mm.addAndReturnMedian(-1.1); // returns -1.1
mm.addAndReturnMedian(9.1); // returns -1.1
mm.addAndReturnMedian(-2.7); // returns -1.1
mm.addAndReturnMedian(-8.1); // returns -2.7
mm.addAndReturnMedian(-7); // returns -2.7
mm.addAndReturnMedian(6.6); // returns -2.7
mm.addAndReturnMedian(6.8); // returns -1.1
```