-
Notifications
You must be signed in to change notification settings - Fork 272
Expand file tree
/
Copy pathMergeSort.java
More file actions
41 lines (40 loc) · 941 Bytes
/
Copy pathMergeSort.java
File metadata and controls
41 lines (40 loc) · 941 Bytes
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
class MergeSort {
void merge(int array[], int start, int middle, int end) {
int n1 = middle - start + 1;
int n2 = end - middle;
int Left[] = new int[n1];
int Right[] = new int[n2];
for (int i = 0; i < n1; ++i) Left[i] = array[start + i];
for (int j = 0; j < n2; ++j) Right[j] = array[middle + 1 + j];
int i = 0, j = 0;
int k = start;
while (i < n1 && j < n2) {
if (Left[i] <= Right[j]) {
array[k] = Left[i];
i++;
} else {
array[k] = Right[j];
j++;
}
k++;
}
while (i < n1) {
array[k] = Left[i];
i++;
k++;
}
while (j < n2) {
array[k] = Right[j];
j++;
k++;
}
}
void sort(int array[], int start, int end) {
if (start < end) {
int middle = (start + end) / 2;
sort(array, start, middle);
sort(array, middle + 1, end);
merge(array, start, middle, end);
}
}
}