Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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))
- Host: GitHub
- URL: https://github.com/ndsvw/median-maintenance
- Owner: ndsvw
- License: mit
- Created: 2019-10-25T18:10:31.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-02-11T07:11:42.000Z (almost 3 years ago)
- Last Synced: 2023-04-29T17:43:06.950Z (over 1 year ago)
- Topics: algorithm, complexity, logarithmic, median
- Language: Java
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
```