forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingInversionsTest.java
More file actions
63 lines (52 loc) · 1.69 KB
/
CountingInversionsTest.java
File metadata and controls
63 lines (52 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.thealgorithms.divideandconquer;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class CountingInversionsTest {
@Test
public void testCountInversions() {
int[] arr = {2, 3, 8, 6, 1};
assertEquals(5, CountingInversions.countInversions(arr));
}
@Test
public void testNoInversions() {
int[] arr = {1, 2, 3, 4, 5};
assertEquals(0, CountingInversions.countInversions(arr));
}
@Test
public void testSingleElement() {
int[] arr = {1};
assertEquals(0, CountingInversions.countInversions(arr));
}
@Test
public void testAllInversions() {
int[] arr = {5, 4, 3, 2, 1};
assertEquals(10, CountingInversions.countInversions(arr));
}
@Test
public void testEmptyArray() {
int[] arr = {};
assertEquals(0, CountingInversions.countInversions(arr));
}
@Test
public void testArrayWithDuplicates() {
int[] arr = {1, 3, 2, 3, 1};
// Inversions: (3,2), (3,1), (3,1), (2,1)
assertEquals(4, CountingInversions.countInversions(arr));
}
@Test
public void testLargeArray() {
int n = 1000;
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = n - i; // descending order -> max inversions = n*(n-1)/2
}
int expected = n * (n - 1) / 2;
assertEquals(expected, CountingInversions.countInversions(arr));
}
@Test
public void testArrayWithAllSameElements() {
int[] arr = {7, 7, 7, 7};
// No inversions since all elements are equal
assertEquals(0, CountingInversions.countInversions(arr));
}
}