mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 02:04:31 +08:00
* #4382 Bug Fix * #4382 Bug Fix * Made Requested Changes * Made Requested Changes * Made Requested Changes * Made Requested Changes * Made Requested Changes * Made Requested Changes * Made Requested Changes * Update src/main/java/com/thealgorithms/misc/MedianOfRunningArray.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update src/main/java/com/thealgorithms/misc/MedianOfRunningArray.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
@ -8,13 +8,13 @@ import java.util.PriorityQueue;
|
|||||||
*/
|
*/
|
||||||
public class MedianOfRunningArray {
|
public class MedianOfRunningArray {
|
||||||
|
|
||||||
private PriorityQueue<Integer> p1;
|
private PriorityQueue<Integer> maxHeap;
|
||||||
private PriorityQueue<Integer> p2;
|
private PriorityQueue<Integer> minHeap;
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
public MedianOfRunningArray() {
|
public MedianOfRunningArray() {
|
||||||
this.p1 = new PriorityQueue<>(Collections.reverseOrder()); // Max Heap
|
this.maxHeap = new PriorityQueue<>(Collections.reverseOrder()); // Max Heap
|
||||||
this.p2 = new PriorityQueue<>(); // Min Heap
|
this.minHeap = new PriorityQueue<>(); // Min Heap
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -22,32 +22,30 @@ public class MedianOfRunningArray {
|
|||||||
and upper half to min heap
|
and upper half to min heap
|
||||||
*/
|
*/
|
||||||
public void insert(Integer e) {
|
public void insert(Integer e) {
|
||||||
p2.add(e);
|
if (!minHeap.isEmpty() && e < minHeap.peek()) {
|
||||||
if (p2.size() - p1.size() > 1) {
|
maxHeap.offer(e);
|
||||||
p1.add(p2.remove());
|
if (maxHeap.size() > minHeap.size() + 1) {
|
||||||
|
minHeap.offer(maxHeap.poll());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
minHeap.offer(e);
|
||||||
|
if (minHeap.size() > maxHeap.size() + 1) {
|
||||||
|
maxHeap.offer(minHeap.poll());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Returns median at any given point
|
Returns median at any given point
|
||||||
*/
|
*/
|
||||||
public Integer median() {
|
public double median() {
|
||||||
if (p1.size() == p2.size()) {
|
if (maxHeap.isEmpty() && minHeap.isEmpty()) {
|
||||||
return (p1.peek() + p2.peek()) / 2;
|
throw new IllegalArgumentException("Enter at least 1 element, Median of empty list is not defined!");
|
||||||
}
|
|
||||||
return p1.size() > p2.size() ? p1.peek() : p2.peek();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
if (maxHeap.size() == minHeap.size()) {
|
||||||
/*
|
return (maxHeap.peek() + minHeap.peek()) / 2.0;
|
||||||
Testing the median function
|
|
||||||
*/
|
|
||||||
|
|
||||||
MedianOfRunningArray p = new MedianOfRunningArray();
|
|
||||||
int[] arr = {10, 7, 4, 9, 2, 3, 11, 17, 14};
|
|
||||||
for (int i = 0; i < 9; i++) {
|
|
||||||
p.insert(arr[i]);
|
|
||||||
System.out.print(p.median() + " ");
|
|
||||||
}
|
}
|
||||||
|
return maxHeap.size() > minHeap.size() ? maxHeap.peek() * 1.0 : minHeap.peek() * 1.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,161 @@
|
|||||||
|
package com.thealgorithms.misc;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.tuple.Pair;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case for Two sum Problem.
|
||||||
|
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class MedianOfRunningArrayTest {
|
||||||
|
private static final String EXCEPTION_MESSAGE = "Enter at least 1 element, Median of empty list is not defined!";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWhenInvalidInoutProvidedShouldThrowException() {
|
||||||
|
MedianOfRunningArray stream = new MedianOfRunningArray();
|
||||||
|
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> stream.median());
|
||||||
|
assertEquals(exception.getMessage(), EXCEPTION_MESSAGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithNegativeValues() {
|
||||||
|
MedianOfRunningArray stream = new MedianOfRunningArray();
|
||||||
|
stream.insert(-1);
|
||||||
|
assertEquals(-1, stream.median());
|
||||||
|
stream.insert(-2);
|
||||||
|
assertEquals(-1.5, stream.median());
|
||||||
|
stream.insert(-3);
|
||||||
|
assertEquals(-2, stream.median());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithSingleValues() {
|
||||||
|
MedianOfRunningArray stream = new MedianOfRunningArray();
|
||||||
|
stream.insert(-1);
|
||||||
|
assertEquals(-1, stream.median());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithRandomValues() {
|
||||||
|
MedianOfRunningArray stream = new MedianOfRunningArray();
|
||||||
|
stream.insert(10);
|
||||||
|
assertEquals(10.0, stream.median());
|
||||||
|
|
||||||
|
stream.insert(5);
|
||||||
|
assertEquals(7.5, stream.median());
|
||||||
|
|
||||||
|
stream.insert(20);
|
||||||
|
assertEquals(10.0, stream.median());
|
||||||
|
|
||||||
|
stream.insert(15);
|
||||||
|
assertEquals(12.5, stream.median());
|
||||||
|
|
||||||
|
stream.insert(25);
|
||||||
|
assertEquals(15.0, stream.median());
|
||||||
|
|
||||||
|
stream.insert(30);
|
||||||
|
assertEquals(17.5, stream.median());
|
||||||
|
|
||||||
|
stream.insert(35);
|
||||||
|
assertEquals(20.0, stream.median());
|
||||||
|
|
||||||
|
stream.insert(1);
|
||||||
|
assertEquals(17.5, stream.median());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithNegativeAndPositiveValues() {
|
||||||
|
MedianOfRunningArray stream = new MedianOfRunningArray();
|
||||||
|
stream.insert(-1);
|
||||||
|
assertEquals(-1, stream.median());
|
||||||
|
stream.insert(2);
|
||||||
|
assertEquals(0.5, stream.median());
|
||||||
|
stream.insert(-3);
|
||||||
|
assertEquals(-1, stream.median());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithDuplicateValues() {
|
||||||
|
MedianOfRunningArray stream = new MedianOfRunningArray();
|
||||||
|
stream.insert(-1);
|
||||||
|
assertEquals(-1, stream.median());
|
||||||
|
stream.insert(-1);
|
||||||
|
assertEquals(-1, stream.median());
|
||||||
|
stream.insert(-1);
|
||||||
|
assertEquals(-1, stream.median());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithDuplicateValuesB() {
|
||||||
|
MedianOfRunningArray stream = new MedianOfRunningArray();
|
||||||
|
stream.insert(10);
|
||||||
|
stream.insert(20);
|
||||||
|
stream.insert(10);
|
||||||
|
stream.insert(10);
|
||||||
|
stream.insert(20);
|
||||||
|
stream.insert(0);
|
||||||
|
stream.insert(50);
|
||||||
|
assertEquals(10, stream.median());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithLargeValues() {
|
||||||
|
MedianOfRunningArray stream = new MedianOfRunningArray();
|
||||||
|
stream.insert(1000000);
|
||||||
|
assertEquals(1000000, stream.median());
|
||||||
|
stream.insert(12000);
|
||||||
|
assertEquals(506000, stream.median());
|
||||||
|
stream.insert(15000000);
|
||||||
|
assertEquals(1000000, stream.median());
|
||||||
|
stream.insert(2300000);
|
||||||
|
assertEquals(1650000.00, stream.median());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithLargeCountOfValues() {
|
||||||
|
MedianOfRunningArray stream = new MedianOfRunningArray();
|
||||||
|
for (int i = 1; i <= 1000; i++) stream.insert(i);
|
||||||
|
assertEquals(500.5, stream.median());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithThreeValuesInDescendingOrder() {
|
||||||
|
MedianOfRunningArray stream = new MedianOfRunningArray();
|
||||||
|
stream.insert(30);
|
||||||
|
stream.insert(20);
|
||||||
|
stream.insert(10);
|
||||||
|
assertEquals(20.0, stream.median());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithThreeValuesInOrder() {
|
||||||
|
MedianOfRunningArray stream = new MedianOfRunningArray();
|
||||||
|
stream.insert(10);
|
||||||
|
stream.insert(20);
|
||||||
|
stream.insert(30);
|
||||||
|
assertEquals(20.0, stream.median());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithThreeValuesNotInOrderA() {
|
||||||
|
MedianOfRunningArray stream = new MedianOfRunningArray();
|
||||||
|
stream.insert(30);
|
||||||
|
stream.insert(10);
|
||||||
|
stream.insert(20);
|
||||||
|
assertEquals(20.0, stream.median());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithThreeValuesNotInOrderB() {
|
||||||
|
MedianOfRunningArray stream = new MedianOfRunningArray();
|
||||||
|
stream.insert(20);
|
||||||
|
stream.insert(10);
|
||||||
|
stream.insert(30);
|
||||||
|
assertEquals(20.0, stream.median());
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user