Skip to content

Commit f361f7d

Browse files
author
Alex Klymenko
committed
refactor: MergeSortNoExtraSpace, change naming, adding test
1 parent 26b4b82 commit f361f7d

File tree

2 files changed

+75
-38
lines changed

2 files changed

+75
-38
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,83 @@
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+
*/
19+
public static int[] sort(int[] array) {
20+
if (array.length == 0) {
21+
return array;
22+
}
23+
int maxElement = Arrays.stream(array).max().getAsInt() + 1;
24+
mergeSort(array, 0, array.length - 1, maxElement);
25+
return array;
1626
}
1727

18-
public static void mergeSort(int[] a, int start, int end, int maxele) { // this function divides the array into 2 halves
28+
/**
29+
* Recursively divides the array into two halves, sorts and merges them.
30+
*
31+
* @param array the array to be sorted
32+
* @param start the starting index of the array
33+
* @param end the ending index of the array
34+
* @param maxElement the value greater than any element in the array, used for encoding
35+
*/
36+
public static void mergeSort(int[] array, int start, int end, int maxElement) {
1937
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);
38+
int middle = (start + end) / 2;
39+
mergeSort(array, start, middle, maxElement);
40+
mergeSort(array, middle + 1, end, maxElement);
41+
merge(array, start, middle, end, maxElement);
2442
}
2543
}
2644

27-
public static void implementMergeSort(int[] a, int start, int mid, int end,
28-
int maxele) { // implementation of mergesort
45+
/**
46+
* Merges two sorted subarrays [start...middle] and [middle+1...end] in place.
47+
*
48+
* @param array the array containing the subarrays to be merged
49+
* @param start the starting index of the first subarray
50+
* @param middle the ending index of the first subarray and starting index of the second subarray
51+
* @param end the ending index of the second subarray
52+
* @param maxElement the value greater than any element in the array, used for encoding
53+
*/
54+
private static void merge(int[] array, int start, int middle, int end, int maxElement) {
2955
int i = start;
30-
int j = mid + 1;
56+
int j = middle + 1;
3157
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;
58+
while (i <= middle && j <= end) {
59+
if (array[i] % maxElement <= array[j] % maxElement) {
60+
array[k] = array[k] + (array[i] % maxElement) * maxElement;
3561
k++;
3662
i++;
3763
} else {
38-
a[k] = a[k] + (a[j] % maxele) * maxele;
64+
array[k] = array[k] + (array[j] % maxElement) * maxElement;
3965
k++;
4066
j++;
4167
}
4268
}
43-
while (i <= mid) {
44-
a[k] = a[k] + (a[i] % maxele) * maxele;
69+
while (i <= middle) {
70+
array[k] = array[k] + (array[i] % maxElement) * maxElement;
4571
k++;
4672
i++;
4773
}
4874
while (j <= end) {
49-
a[k] = a[k] + (a[j] % maxele) * maxele;
75+
array[k] = array[k] + (array[j] % maxElement) * maxElement;
5076
k++;
5177
j++;
5278
}
5379
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] + " ");
80+
array[i] = array[i] / maxElement;
7081
}
71-
inp.close();
7282
}
7383
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.thealgorithms.sorts;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.MethodSource;
5+
6+
import java.util.stream.Stream;
7+
8+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
9+
10+
public class MergeSortNoExtraSpaceTest {
11+
record TestCase(int[] inputArray, int[] expectedArray) {
12+
}
13+
14+
static Stream<TestCase> provideTestCases() {
15+
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}), 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}),
16+
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}), 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}),
17+
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[] {}, new int[] {}), new TestCase(new int[] {1}, new int[] {1}), new TestCase(new int[] {2, 1}, new int[] {1, 2}), new TestCase(new int[] {1, 3, 2}, new int[] {1, 2, 3})
18+
);
19+
}
20+
21+
@ParameterizedTest
22+
@MethodSource("provideTestCases")
23+
public void testCountingSort(TestCase testCase) {
24+
int[] outputArray = MergeSortNoExtraSpace.sort(testCase.inputArray);
25+
assertArrayEquals(testCase.expectedArray, outputArray);
26+
}
27+
}

0 commit comments

Comments
 (0)