Skip to content

Commit 57f6580

Browse files
alxkmAlex Klymenkovil02
authored
refactor: MergeSortNoExtraSpace (#5277)
* refactor: MergeSortNoExtraSpace, change naming, adding test * checkstyle: fix import ordering, and formatting * fix: adding negative numbers check, fix possible overflow * checkstyle: remove newline --------- Co-authored-by: Alex Klymenko <[email protected]> Co-authored-by: vil02 <[email protected]>
1 parent 1426460 commit 57f6580

File tree

2 files changed

+87
-38
lines changed

2 files changed

+87
-38
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,88 @@
11
package com.thealgorithms.sorts;
22

33
import java.util.Arrays;
4-
import java.util.Scanner;
54

6-
/*This code implements the mergeSort algorithm without extra space
7-
For understanding about mergesort visit :https://www.geeksforgeeks.org/merge-sort/
5+
/**
6+
* Implementation of Merge Sort without using extra space for merging.
7+
* This implementation performs in-place merging to sort the array of integers.
88
*/
99
public final class MergeSortNoExtraSpace {
1010
private MergeSortNoExtraSpace() {
1111
}
1212

13-
public static void callMergeSort(int[] a, int n) {
14-
int maxele = Arrays.stream(a).max().getAsInt() + 1;
15-
mergeSort(a, 0, n - 1, maxele);
13+
/**
14+
* Sorts the array using in-place merge sort algorithm.
15+
*
16+
* @param array the array to be sorted
17+
* @return the sorted array
18+
* @throws IllegalArgumentException If the array contains negative numbers.
19+
*/
20+
public static int[] sort(int[] array) {
21+
if (array.length == 0) {
22+
return array;
23+
}
24+
if (Arrays.stream(array).anyMatch(s -> s < 0)) {
25+
throw new IllegalArgumentException("Implementation cannot sort negative numbers.");
26+
}
27+
28+
final int maxElement = Arrays.stream(array).max().getAsInt() + 1;
29+
mergeSort(array, 0, array.length - 1, maxElement);
30+
return array;
1631
}
1732

18-
public static void mergeSort(int[] a, int start, int end, int maxele) { // this function divides the array into 2 halves
33+
/**
34+
* Recursively divides the array into two halves, sorts and merges them.
35+
*
36+
* @param array the array to be sorted
37+
* @param start the starting index of the array
38+
* @param end the ending index of the array
39+
* @param maxElement the value greater than any element in the array, used for encoding
40+
*/
41+
public static void mergeSort(int[] array, int start, int end, int maxElement) {
1942
if (start < end) {
20-
int mid = (start + end) / 2;
21-
mergeSort(a, start, mid, maxele);
22-
mergeSort(a, mid + 1, end, maxele);
23-
implementMergeSort(a, start, mid, end, maxele);
43+
final int middle = (start + end) >>> 1;
44+
mergeSort(array, start, middle, maxElement);
45+
mergeSort(array, middle + 1, end, maxElement);
46+
merge(array, start, middle, end, maxElement);
2447
}
2548
}
2649

27-
public static void implementMergeSort(int[] a, int start, int mid, int end,
28-
int maxele) { // implementation of mergesort
50+
/**
51+
* Merges two sorted subarrays [start...middle] and [middle+1...end] in place.
52+
*
53+
* @param array the array containing the subarrays to be merged
54+
* @param start the starting index of the first subarray
55+
* @param middle the ending index of the first subarray and starting index of the second subarray
56+
* @param end the ending index of the second subarray
57+
* @param maxElement the value greater than any element in the array, used for encoding
58+
*/
59+
private static void merge(int[] array, int start, int middle, int end, int maxElement) {
2960
int i = start;
30-
int j = mid + 1;
61+
int j = middle + 1;
3162
int k = start;
32-
while (i <= mid && j <= end) {
33-
if (a[i] % maxele <= a[j] % maxele) {
34-
a[k] = a[k] + (a[i] % maxele) * maxele;
63+
while (i <= middle && j <= end) {
64+
if (array[i] % maxElement <= array[j] % maxElement) {
65+
array[k] = array[k] + (array[i] % maxElement) * maxElement;
3566
k++;
3667
i++;
3768
} else {
38-
a[k] = a[k] + (a[j] % maxele) * maxele;
69+
array[k] = array[k] + (array[j] % maxElement) * maxElement;
3970
k++;
4071
j++;
4172
}
4273
}
43-
while (i <= mid) {
44-
a[k] = a[k] + (a[i] % maxele) * maxele;
74+
while (i <= middle) {
75+
array[k] = array[k] + (array[i] % maxElement) * maxElement;
4576
k++;
4677
i++;
4778
}
4879
while (j <= end) {
49-
a[k] = a[k] + (a[j] % maxele) * maxele;
80+
array[k] = array[k] + (array[j] % maxElement) * maxElement;
5081
k++;
5182
j++;
5283
}
5384
for (i = start; i <= end; i++) {
54-
a[i] = a[i] / maxele;
55-
}
56-
}
57-
58-
public static void main(String[] args) {
59-
Scanner inp = new Scanner(System.in);
60-
System.out.println("Enter array size");
61-
int n = inp.nextInt();
62-
int[] a = new int[n];
63-
System.out.println("Enter array elements");
64-
for (int i = 0; i < n; i++) {
65-
a[i] = inp.nextInt();
66-
}
67-
callMergeSort(a, n);
68-
for (int i = 0; i < a.length; i++) {
69-
System.out.print(a[i] + " ");
85+
array[i] = array[i] / maxElement;
7086
}
71-
inp.close();
7287
}
7388
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.thealgorithms.sorts;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
public class MergeSortNoExtraSpaceTest {
12+
record TestCase(int[] inputArray, int[] expectedArray) {
13+
}
14+
15+
static Stream<TestCase> provideTestCases() {
16+
return Stream.of(new TestCase(new int[] {}, new int[] {}), new TestCase(new int[] {1}, new int[] {1}), new TestCase(new int[] {1, 2, 3, 4, 5}, new int[] {1, 2, 3, 4, 5}), new TestCase(new int[] {5, 4, 3, 2, 1}, new int[] {1, 2, 3, 4, 5}),
17+
new TestCase(new int[] {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}, new int[] {1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9}), new TestCase(new int[] {4, 2, 4, 3, 2, 1, 5}, new int[] {1, 2, 2, 3, 4, 4, 5}), new TestCase(new int[] {0, 0, 0, 0}, new int[] {0, 0, 0, 0}),
18+
new TestCase(new int[] {1000, 500, 100, 50, 10, 5, 1}, new int[] {1, 5, 10, 50, 100, 500, 1000}), new TestCase(new int[] {1, 2, 3, 1, 2, 3, 1, 2, 3}, new int[] {1, 1, 1, 2, 2, 2, 3, 3, 3}),
19+
new TestCase(new int[] {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}), new TestCase(new int[] {2, 1}, new int[] {1, 2}), new TestCase(new int[] {1, 3, 2}, new int[] {1, 2, 3}));
20+
}
21+
22+
@ParameterizedTest
23+
@MethodSource("provideTestCases")
24+
public void testCountingSort(TestCase testCase) {
25+
int[] outputArray = MergeSortNoExtraSpace.sort(testCase.inputArray);
26+
assertArrayEquals(testCase.expectedArray, outputArray);
27+
}
28+
29+
@Test
30+
public void testNegativeNumbers() {
31+
int[] arrayWithNegatives = {1, -2, 3, -4};
32+
assertThrows(IllegalArgumentException.class, () -> MergeSortNoExtraSpace.sort(arrayWithNegatives));
33+
}
34+
}

0 commit comments

Comments
 (0)