-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort.java
60 lines (54 loc) · 1.59 KB
/
MergeSort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package sorting;
import java.util.Arrays;
public class MergeSort {
/**
* Time Complexity: O(n log n)
* Space Complexity: O(n)
* @param input
*/
public static void sort(int[] input){
mergeSort(0,input.length-1,input);
}
private static void mergeSort(int left, int right, int[] input){
if(left < right){
int mid = left + (right-left)/2;
mergeSort(left, mid, input);
mergeSort(mid+1, right,input);
merge(left,mid,right, input);
}
}
private static void merge(int left, int mid, int right, int[] input){
int m = mid - left + 1;
int n = right - mid;
int[] leftTemp = new int[m];
int[] rightTemp = new int[n];
for(int i=0;i<m;i++){
leftTemp[i] = input[left+i];
}
for(int i=0;i<n;i++){
rightTemp[i] = input[mid+1+i];
}
int i = 0;
int j = 0;
int index = left;
while(i<m && j<n){
if(leftTemp[i] <= rightTemp[j]){
input[index++] = leftTemp[i++];
}else{
input[index++] = rightTemp[j++];
}
}
while(i<m){
input[index++] = leftTemp[i++];
}
while(j<n){
input[index++] = rightTemp[j++];
}
}
public static void main(String[] args){
int[] input = new int[]{3,6,7,5,1,2,9,4,8};
System.out.println("Before Sorting: " + Arrays.toString(input));
MergeSort.sort(input);
System.out.println("After Sorting: " + Arrays.toString(input));
}
}