Skip to content

Commit 5d428d0

Browse files
authored
Add MedianFinder algorithm (#5837)
1 parent d572997 commit 5d428d0

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
* [KthElementFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/KthElementFinder.java)
178178
* [LeftistHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java)
179179
* [MaxHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java)
180+
* [MedianFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MedianFinder.java)
180181
* [MinHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java)
181182
* [MinPriorityQueue](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MinPriorityQueue.java)
182183
* lists
@@ -851,6 +852,7 @@
851852
* [GenericHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java)
852853
* [KthElementFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/KthElementFinderTest.java)
853854
* [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
855+
* [MedianFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/MedianFinderTest.java)
854856
* [MinPriorityQueueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/MinPriorityQueueTest.java)
855857
* lists
856858
* [CircleLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.thealgorithms.datastructures.heaps;
2+
3+
import java.util.PriorityQueue;
4+
5+
/**
6+
* This class maintains the median of a dynamically changing data stream using
7+
* two heaps: a max-heap and a min-heap. The max-heap stores the smaller half
8+
* of the numbers, and the min-heap stores the larger half.
9+
* This data structure ensures that retrieving the median is efficient.
10+
*
11+
* Time Complexity:
12+
* - Adding a number: O(log n) due to heap insertion.
13+
* - Finding the median: O(1).
14+
*
15+
* Space Complexity: O(n), where n is the total number of elements added.
16+
*
17+
* @author Hardvan
18+
*/
19+
public final class MedianFinder {
20+
MedianFinder() {
21+
}
22+
23+
private PriorityQueue<Integer> minHeap = new PriorityQueue<>();
24+
private PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
25+
26+
/**
27+
* Adds a new number to the data stream. The number is placed in the appropriate
28+
* heap to maintain the balance between the two heaps.
29+
*
30+
* @param num the number to be added to the data stream
31+
*/
32+
public void addNum(int num) {
33+
if (maxHeap.isEmpty() || num <= maxHeap.peek()) {
34+
maxHeap.offer(num);
35+
} else {
36+
minHeap.offer(num);
37+
}
38+
39+
if (maxHeap.size() > minHeap.size() + 1) {
40+
minHeap.offer(maxHeap.poll());
41+
} else if (minHeap.size() > maxHeap.size()) {
42+
maxHeap.offer(minHeap.poll());
43+
}
44+
}
45+
46+
/**
47+
* Finds the median of the numbers added so far. If the total number of elements
48+
* is even, the median is the average of the two middle elements. If odd, the
49+
* median is the middle element from the max-heap.
50+
*
51+
* @return the median of the numbers in the data stream
52+
*/
53+
public double findMedian() {
54+
if (maxHeap.size() == minHeap.size()) {
55+
return (maxHeap.peek() + minHeap.peek()) / 2.0;
56+
}
57+
return maxHeap.peek();
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.thealgorithms.datastructures.heaps;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class MedianFinderTest {
8+
@Test
9+
public void testMedianMaintenance() {
10+
MedianFinder mf = new MedianFinder();
11+
mf.addNum(1);
12+
mf.addNum(2);
13+
assertEquals(1.5, mf.findMedian());
14+
mf.addNum(3);
15+
assertEquals(2.0, mf.findMedian());
16+
}
17+
}

0 commit comments

Comments
 (0)