-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMergeSort.java
58 lines (52 loc) · 1.43 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
public class MergeSort {
public static void main(String args[]) {
//input array
int[] input = { 12,98,88,55,89,5,4,1,-1 };
int[] arr = sort(input);
//printing the sorted array
for (int i : arr) {
System.out.println(i);
}
}
public static int[] sort(int[] arr) {
int length = arr.length;
divideArray(arr, 0, length - 1);
return arr;
}
public static int[] divideArray(int[] arr, int lowerIndex, int higherIndex) {
if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
// recursion for solving the left partition
divideArray(arr, lowerIndex, middle);
// recursion for solving the right partition
divideArray(arr, middle + 1, higherIndex);
// merge both the left and right arrays to get the result
mergeArray(arr, lowerIndex, middle, higherIndex);
}
return arr;
}
public static int[] mergeArray(int[] arr, int lowerIndex, int middle, int higherIndex) {
int[] tempArr = new int[arr.length];
System.arraycopy(arr, 0, tempArr, 0, arr.length);
int start = lowerIndex;
int end = middle + 1;
int i = lowerIndex;
;
while (start <= middle && end <= higherIndex) {
if (tempArr[start] <= tempArr[end]) {
arr[i] = tempArr[start];
start++;
} else {
arr[i] = tempArr[end];
end++;
}
i++;
}
while (start <= middle) {
arr[i] = tempArr[start];
i++;
start++;
}
return arr;
}
}