Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8363578

Browse files
committed
MergeSort space optimized made simple
1 parent c41d117 commit 8363578

File tree

2 files changed

+40
-41
lines changed

2 files changed

+40
-41
lines changed

src/main/java/com/rampatra/sorting/MergeSort.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ public class MergeSort {
1919
* Time complexity: O(n log n)
2020
* Space complexity: O(n) (also needs O(log n) stack space as it is recursive)
2121
*
22-
* @param a
22+
* @param arr
2323
* @return
2424
*/
25-
public static int[] mergeSort(int[] a) {
26-
if (a.length == 1) return a;
25+
public static int[] mergeSort(int[] arr) {
26+
if (arr.length == 1) return arr;
2727

28-
int[] x = mergeSort(Arrays.copyOfRange(a, 0, a.length / 2));
29-
int[] y = mergeSort(Arrays.copyOfRange(a, a.length / 2, a.length));
28+
int[] x = mergeSort(Arrays.copyOfRange(arr, 0, arr.length / 2));
29+
int[] y = mergeSort(Arrays.copyOfRange(arr, arr.length / 2, arr.length));
3030

3131
return merge(x, y);
3232
}
3333

3434
/**
35-
* Merges two sorted arrays {@param a} and {@param b}.
35+
* Merges two sorted arrays {@code a} and {@code b}.
3636
*
3737
* @param a
3838
* @param b

src/main/java/com/rampatra/sorting/MergeSortSpaceOptimized.java

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,59 +10,58 @@ public class MergeSortSpaceOptimized {
1010

1111
/**
1212
* This is entry point. You can call this method to sort
13-
* an array {@code a}.
13+
* an array {@code arr}.
1414
*
15-
* @param a array to be sorted
15+
* @param arr array to be sorted
1616
*/
17-
public static void mergeSort(int[] a) {
18-
mergeSort(a, new int[a.length], 0, a.length - 1);
17+
public static void mergeSort(int[] arr) {
18+
mergeSort(arr, new int[arr.length], 0, arr.length - 1);
1919
}
2020

21-
private static void mergeSort(int[] a, int[] helper, int low, int high) {
21+
private static void mergeSort(int[] arr, int[] helper, int low, int high) {
2222
if (low < high) {
2323
int mid = (low + high) / 2; // to prevent overflow you can instead do: mid = low + (high - low) / 2
2424

25-
mergeSort(a, helper, low, mid);
26-
mergeSort(a, helper, mid + 1, high);
25+
mergeSort(arr, helper, low, mid);
26+
mergeSort(arr, helper, mid + 1, high);
2727

28-
merge(a, helper, low, mid, high);
28+
merge(arr, helper, low, mid, high);
2929
}
3030
}
3131

32-
private static void merge(int[] a, int[] helper, int low, int mid, int high) {
33-
// have a helper array from which you will choose numbers and finally place it in a
34-
for (int i = low; i <= high; i++) {
35-
helper[i] = a[i];
36-
}
37-
38-
int helperLeft = low;
39-
int helperRight = mid + 1;
40-
int current = low;
32+
private static void merge(int[] arr, int[] helper, int low, int mid, int high) {
33+
int k = low;
4134

42-
// check left half of the helper array with the right half
43-
while (helperLeft <= mid && helperRight <= high) {
44-
if (helper[helperLeft] < helper[helperRight]) {
45-
a[current++] = helper[helperLeft++];
46-
} else {
47-
a[current++] = helper[helperRight++];
35+
/*
36+
Similar to merging two sorted arrays, i.e, merge two parts of arr[], arr[low..mid] and arr[mid+1..high],
37+
and store in helper[]
38+
*/
39+
for (int i = low, j = mid + 1; i <= mid || j <= high; ) {
40+
if (i > mid) {
41+
helper[k++] = arr[j++];
42+
} else if (j > high) {
43+
helper[k++] = arr[i++];
44+
} else if (arr[i] <= arr[j]) {
45+
helper[k++] = arr[i++];
46+
} else if (arr[i] > arr[j]) {
47+
helper[k++] = arr[j++];
4848
}
4949
}
5050

51-
// copy the left half of the helper array and not the right
52-
// half as the right half is already there in array a
53-
for (int i = helperLeft; i <= mid; i++) {
54-
a[current++] = helper[i];
51+
// finally copy the sorted result from helper[] to arr[]
52+
for (int i = low; i <= high; i++) { // note: we can use System.arraycopy() for better performance
53+
arr[i] = helper[i];
5554
}
5655
}
5756

5857
public static void main(String[] args) {
59-
int[] a = {3, 6, 8, 9, 1, 2, 4};
60-
System.out.println(Arrays.toString(a));
61-
mergeSort(a);
62-
System.out.println(Arrays.toString(a));
63-
a = new int[]{5, 8, 1, 2, 5, 3, 0, 1, 2, 4};
64-
System.out.println(Arrays.toString(a));
65-
mergeSort(a);
66-
System.out.println(Arrays.toString(a));
58+
int[] arr = {3, 6, 8, 9, 1, 2, 4};
59+
System.out.println(Arrays.toString(arr));
60+
mergeSort(arr);
61+
System.out.println(Arrays.toString(arr));
62+
arr = new int[]{5, 8, 1, 2, 5, 3, 0, 1, 2, 4};
63+
System.out.println(Arrays.toString(arr));
64+
mergeSort(arr);
65+
System.out.println(Arrays.toString(arr));
6766
}
6867
}

0 commit comments

Comments
 (0)