@@ -10,59 +10,58 @@ public class MergeSortSpaceOptimized {
10
10
11
11
/**
12
12
* This is entry point. You can call this method to sort
13
- * an array {@code a }.
13
+ * an array {@code arr }.
14
14
*
15
- * @param a array to be sorted
15
+ * @param arr array to be sorted
16
16
*/
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 );
19
19
}
20
20
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 ) {
22
22
if (low < high ) {
23
23
int mid = (low + high ) / 2 ; // to prevent overflow you can instead do: mid = low + (high - low) / 2
24
24
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 );
27
27
28
- merge (a , helper , low , mid , high );
28
+ merge (arr , helper , low , mid , high );
29
29
}
30
30
}
31
31
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 ;
41
34
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 ++];
48
48
}
49
49
}
50
50
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 ];
55
54
}
56
55
}
57
56
58
57
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 ));
67
66
}
68
67
}
0 commit comments