-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort.java
49 lines (45 loc) · 1.37 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
package array;
import java.util.Arrays;
public class MergeSort {
/**
* Main method of the class for the following question:
* Merge Sort Algorithm to sort an array
* */
public static void main(String[] args) {
int[] arr = {5, 4, 3, 2, 10, 1, 9, 7, 8, 6};
int[] sorted = mergeSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(sorted));
}
private static int[] mergeSort(int[] arr, int start, int end) {
if (start < end) {
int mid = (start + end) / 2;
mergeSort(arr, start, mid);
mergeSort(arr, mid + 1, end);
merge(arr, start, mid, end);
}
return arr;
}
private static void merge(int[] arr, int start, int mid, int end) {
int[] temp = new int[end - start + 1];
int i = start, j = mid + 1, k = 0;
while (i <= mid && j <= end) {
if (arr[i] < arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= end) {
temp[k++] = arr[j++];
}
/* Old method
for (int l = 0; l < temp.length; l++) {
arr[start + l] = temp[l];
}
*/
System.arraycopy(temp, 0, arr, start, temp.length);
}
}