Skip to content

Commit 1053e93

Browse files
authored
Merge branch 'master' into patch-2
2 parents 285365f + 69a1424 commit 1053e93

File tree

5 files changed

+172
-5
lines changed

5 files changed

+172
-5
lines changed

pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>org.junit</groupId>
2222
<artifactId>junit-bom</artifactId>
23-
<version>5.11.2</version>
23+
<version>5.11.3</version>
2424
<type>pom</type>
2525
<scope>import</scope>
2626
</dependency>
@@ -31,7 +31,7 @@
3131
<dependency>
3232
<groupId>org.junit.jupiter</groupId>
3333
<artifactId>junit-jupiter</artifactId>
34-
<version>5.11.2</version>
34+
<version>5.11.3</version>
3535
<scope>test</scope>
3636
</dependency>
3737
<dependency>
@@ -43,15 +43,15 @@
4343
<dependency>
4444
<groupId>org.mockito</groupId>
4545
<artifactId>mockito-core</artifactId>
46-
<version>5.14.1</version>
46+
<version>5.14.2</version>
4747
<scope>test</scope>
4848
</dependency>
4949

5050

5151
<dependency>
5252
<groupId>org.junit.jupiter</groupId>
5353
<artifactId>junit-jupiter-api</artifactId>
54-
<version>5.11.2</version>
54+
<version>5.11.3</version>
5555
<scope>test</scope>
5656
</dependency>
5757
<dependency>
@@ -132,7 +132,7 @@
132132
<plugin>
133133
<groupId>com.github.spotbugs</groupId>
134134
<artifactId>spotbugs-maven-plugin</artifactId>
135-
<version>4.8.6.4</version>
135+
<version>4.8.6.5</version>
136136
<configuration>
137137
<excludeFilterFile>spotbugs-exclude.xml</excludeFilterFile>
138138
<includeTests>true</includeTests>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.thealgorithms.sorts;
2+
3+
public class AdaptiveMergeSort implements SortAlgorithm {
4+
@SuppressWarnings("unchecked")
5+
public <T extends Comparable<T>> T[] sort(T[] array) {
6+
if (array.length <= 1) {
7+
return array;
8+
}
9+
T[] aux = array.clone();
10+
sort(array, aux, 0, array.length - 1);
11+
return array;
12+
}
13+
14+
private <T extends Comparable<T>> void sort(T[] array, T[] aux, int low, int high) {
15+
if (low >= high) {
16+
return;
17+
}
18+
int mid = low + (high - low) / 2;
19+
sort(array, aux, low, mid);
20+
sort(array, aux, mid + 1, high);
21+
merge(array, aux, low, mid, high);
22+
}
23+
24+
private <T extends Comparable<T>> void merge(T[] array, T[] aux, int low, int mid, int high) {
25+
System.arraycopy(array, low, aux, low, high - low + 1);
26+
int i = low;
27+
int j = mid + 1;
28+
for (int k = low; k <= high; k++) {
29+
if (i > mid) {
30+
array[k] = aux[j++];
31+
} else if (j > high) {
32+
array[k] = aux[i++];
33+
} else if (aux[j].compareTo(aux[i]) < 0) {
34+
array[k] = aux[j++];
35+
} else {
36+
array[k] = aux[i++];
37+
}
38+
}
39+
}
40+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.thealgorithms.sorts;
2+
3+
public class StalinSort implements SortAlgorithm {
4+
@SuppressWarnings("unchecked")
5+
public <T extends Comparable<T>> T[] sort(T[] array) {
6+
if (array.length == 0) {
7+
return array;
8+
}
9+
int currentIndex = 0;
10+
for (int i = 1; i < array.length; i++) {
11+
if (array[i].compareTo(array[currentIndex]) >= 0) {
12+
currentIndex++;
13+
array[currentIndex] = array[i];
14+
}
15+
}
16+
// Create a result array with sorted elements
17+
T[] result = (T[]) java.lang.reflect.Array.newInstance(array.getClass().getComponentType(), currentIndex + 1);
18+
System.arraycopy(array, 0, result, 0, currentIndex + 1);
19+
return result;
20+
}
21+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.thealgorithms.sorts;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class AdaptiveMergeSortTest {
8+
9+
@Test
10+
public void testSortIntegers() {
11+
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort();
12+
Integer[] input = {4, 23, 6, 78, 1, 54, 231, 9, 12};
13+
Integer[] expected = {1, 4, 6, 9, 12, 23, 54, 78, 231};
14+
Integer[] result = adaptiveMergeSort.sort(input);
15+
assertArrayEquals(expected, result);
16+
}
17+
18+
@Test
19+
public void testSortStrings() {
20+
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort();
21+
String[] input = {"c", "a", "e", "b", "d"};
22+
String[] expected = {"a", "b", "c", "d", "e"};
23+
String[] result = adaptiveMergeSort.sort(input);
24+
assertArrayEquals(expected, result);
25+
}
26+
27+
@Test
28+
public void testSortWithDuplicates() {
29+
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort();
30+
Integer[] input = {1, 3, 2, 2, 5, 4};
31+
Integer[] expected = {1, 2, 2, 3, 4, 5};
32+
Integer[] result = adaptiveMergeSort.sort(input);
33+
assertArrayEquals(expected, result);
34+
}
35+
36+
@Test
37+
public void testSortEmptyArray() {
38+
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort();
39+
Integer[] input = {};
40+
Integer[] expected = {};
41+
Integer[] result = adaptiveMergeSort.sort(input);
42+
assertArrayEquals(expected, result);
43+
}
44+
45+
@Test
46+
public void testSortSingleElement() {
47+
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort();
48+
Integer[] input = {42};
49+
Integer[] expected = {42};
50+
Integer[] result = adaptiveMergeSort.sort(input);
51+
assertArrayEquals(expected, result);
52+
}
53+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.thealgorithms.sorts;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class StalinSortTest {
8+
9+
@Test
10+
public void testSortIntegers() {
11+
StalinSort stalinSort = new StalinSort();
12+
Integer[] input = {4, 23, 6, 78, 1, 54, 231, 9, 12};
13+
Integer[] expected = {4, 23, 78, 231};
14+
Integer[] result = stalinSort.sort(input);
15+
assertArrayEquals(expected, result);
16+
}
17+
18+
@Test
19+
public void testSortStrings() {
20+
StalinSort stalinSort = new StalinSort();
21+
String[] input = {"c", "a", "e", "b", "d"};
22+
String[] expected = {"c", "e"};
23+
String[] result = stalinSort.sort(input);
24+
assertArrayEquals(expected, result);
25+
}
26+
27+
@Test
28+
public void testSortWithDuplicates() {
29+
StalinSort stalinSort = new StalinSort();
30+
Integer[] input = {1, 3, 2, 2, 5, 4};
31+
Integer[] expected = {1, 3, 5};
32+
Integer[] result = stalinSort.sort(input);
33+
assertArrayEquals(expected, result);
34+
}
35+
36+
@Test
37+
public void testSortEmptyArray() {
38+
StalinSort stalinSort = new StalinSort();
39+
Integer[] input = {};
40+
Integer[] expected = {};
41+
Integer[] result = stalinSort.sort(input);
42+
assertArrayEquals(expected, result);
43+
}
44+
45+
@Test
46+
public void testSortSingleElement() {
47+
StalinSort stalinSort = new StalinSort();
48+
Integer[] input = {42};
49+
Integer[] expected = {42};
50+
Integer[] result = stalinSort.sort(input);
51+
assertArrayEquals(expected, result);
52+
}
53+
}

0 commit comments

Comments
 (0)