Skip to content

Commit ff9fd2e

Browse files
Hardvanalxkm
andauthored
refactor: Enhance docs, code, add tests in Median (TheAlgorithms#6751)
Co-authored-by: a <alexanderklmn@gmail.com>
1 parent bc7b656 commit ff9fd2e

File tree

2 files changed

+148
-13
lines changed

2 files changed

+148
-13
lines changed

src/main/java/com/thealgorithms/maths/Median.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,35 @@
33
import java.util.Arrays;
44

55
/**
6-
* Wikipedia: https://en.wikipedia.org/wiki/Median
6+
* Utility class for calculating the median of an array of integers.
7+
* The median is the middle value in a sorted list of numbers. If the list has
8+
* an even number
9+
* of elements, the median is the average of the two middle values.
10+
*
11+
* <p>
12+
* Time Complexity: O(n log n) due to sorting
13+
* <p>
14+
* Space Complexity: O(1) if sorting is done in-place
15+
*
16+
* @see <a href="https://en.wikipedia.org/wiki/Median">Median (Wikipedia)</a>
17+
* @see <a href=
18+
* "https://www.khanacademy.org/math/statistics-probability/summarizing-quantitative-data/mean-median-basics/a/mean-median-and-mode-review">Mean,
19+
* Median, and Mode Review</a>
720
*/
821
public final class Median {
922
private Median() {
1023
}
1124

1225
/**
13-
* Calculate average median
14-
* @param values sorted numbers to find median of
15-
* @return median of given {@code values}
16-
* @throws IllegalArgumentException If the input array is empty or null.
26+
* Calculates the median of an array of integers.
27+
* The array is sorted internally, so the original order is not preserved.
28+
* For arrays with an odd number of elements, returns the middle element.
29+
* For arrays with an even number of elements, returns the average of the two
30+
* middle elements.
31+
*
32+
* @param values the array of integers to find the median of (can be unsorted)
33+
* @return the median value as a double
34+
* @throws IllegalArgumentException if the input array is empty or null
1735
*/
1836
public static double median(int[] values) {
1937
if (values == null || values.length == 0) {
@@ -22,6 +40,10 @@ public static double median(int[] values) {
2240

2341
Arrays.sort(values);
2442
int length = values.length;
25-
return length % 2 == 0 ? (values[length / 2] + values[length / 2 - 1]) / 2.0 : values[length / 2];
43+
if (length % 2 == 0) {
44+
return (values[length / 2] + values[length / 2 - 1]) / 2.0;
45+
} else {
46+
return values[length / 2];
47+
}
2648
}
2749
}

src/test/java/com/thealgorithms/maths/MedianTest.java

Lines changed: 120 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,153 @@
55

66
import org.junit.jupiter.api.Test;
77

8-
public class MedianTest {
8+
/**
9+
* Test class for {@link Median}.
10+
* Tests various scenarios including edge cases, odd/even length arrays,
11+
* negative values, and unsorted inputs.
12+
*/
13+
class MedianTest {
14+
915
@Test
10-
void medianSingleValue() {
16+
void testMedianSingleValue() {
1117
int[] arr = {0};
1218
assertEquals(0, Median.median(arr));
1319
}
1420

1521
@Test
16-
void medianTwoValues() {
22+
void testMedianSinglePositiveValue() {
23+
int[] arr = {42};
24+
assertEquals(42, Median.median(arr));
25+
}
26+
27+
@Test
28+
void testMedianSingleNegativeValue() {
29+
int[] arr = {-15};
30+
assertEquals(-15, Median.median(arr));
31+
}
32+
33+
@Test
34+
void testMedianTwoValues() {
1735
int[] arr = {1, 2};
1836
assertEquals(1.5, Median.median(arr));
1937
}
2038

2139
@Test
22-
void medianThreeValues() {
40+
void testMedianTwoIdenticalValues() {
41+
int[] arr = {5, 5};
42+
assertEquals(5.0, Median.median(arr));
43+
}
44+
45+
@Test
46+
void testMedianThreeValues() {
2347
int[] arr = {1, 2, 3};
2448
assertEquals(2, Median.median(arr));
2549
}
2650

2751
@Test
28-
void medianDecimalValueReturn() {
52+
void testMedianThreeUnsortedValues() {
53+
int[] arr = {3, 1, 2};
54+
assertEquals(2, Median.median(arr));
55+
}
56+
57+
@Test
58+
void testMedianDecimalValueReturn() {
2959
int[] arr = {1, 2, 3, 4, 5, 6, 8, 9};
3060
assertEquals(4.5, Median.median(arr));
3161
}
3262

3363
@Test
34-
void medianNegativeValues() {
64+
void testMedianNegativeValues() {
3565
int[] arr = {-27, -16, -7, -4, -2, -1};
3666
assertEquals(-5.5, Median.median(arr));
3767
}
3868

3969
@Test
40-
void medianEmptyArrayThrows() {
70+
void testMedianMixedPositiveAndNegativeValues() {
71+
int[] arr = {-10, -5, 0, 5, 10};
72+
assertEquals(0, Median.median(arr));
73+
}
74+
75+
@Test
76+
void testMedianMixedUnsortedValues() {
77+
int[] arr = {10, -5, 0, 5, -10};
78+
assertEquals(0, Median.median(arr));
79+
}
80+
81+
@Test
82+
void testMedianLargeOddArray() {
83+
int[] arr = {9, 7, 5, 3, 1, 2, 4, 6, 8};
84+
assertEquals(5, Median.median(arr));
85+
}
86+
87+
@Test
88+
void testMedianLargeEvenArray() {
89+
int[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
90+
assertEquals(55.0, Median.median(arr));
91+
}
92+
93+
@Test
94+
void testMedianAllSameValues() {
95+
int[] arr = {7, 7, 7, 7, 7};
96+
assertEquals(7.0, Median.median(arr));
97+
}
98+
99+
@Test
100+
void testMedianWithZeros() {
101+
int[] arr = {0, 0, 0, 0, 0};
102+
assertEquals(0.0, Median.median(arr));
103+
}
104+
105+
@Test
106+
void testMedianAlreadySorted() {
107+
int[] arr = {1, 2, 3, 4, 5};
108+
assertEquals(3, Median.median(arr));
109+
}
110+
111+
@Test
112+
void testMedianReverseSorted() {
113+
int[] arr = {5, 4, 3, 2, 1};
114+
assertEquals(3, Median.median(arr));
115+
}
116+
117+
@Test
118+
void testMedianWithDuplicates() {
119+
int[] arr = {1, 2, 2, 3, 3, 3, 4};
120+
assertEquals(3, Median.median(arr));
121+
}
122+
123+
@Test
124+
void testMedianEmptyArrayThrows() {
41125
int[] arr = {};
42126
assertThrows(IllegalArgumentException.class, () -> Median.median(arr));
43127
}
128+
129+
@Test
130+
void testMedianNullArrayThrows() {
131+
assertThrows(IllegalArgumentException.class, () -> Median.median(null));
132+
}
133+
134+
@Test
135+
void testMedianExtremeValues() {
136+
int[] arr = {Integer.MAX_VALUE, Integer.MIN_VALUE};
137+
assertEquals(-0.5, Median.median(arr));
138+
}
139+
140+
@Test
141+
void testMedianTwoNegativeValues() {
142+
int[] arr = {-10, -20};
143+
assertEquals(-15.0, Median.median(arr));
144+
}
145+
146+
@Test
147+
void testMedianFourValuesEven() {
148+
int[] arr = {1, 2, 3, 4};
149+
assertEquals(2.5, Median.median(arr));
150+
}
151+
152+
@Test
153+
void testMedianFiveValuesOdd() {
154+
int[] arr = {10, 20, 30, 40, 50};
155+
assertEquals(30.0, Median.median(arr));
156+
}
44157
}

0 commit comments

Comments
 (0)