Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
34 lines (32 loc) · 1021 Bytes

File metadata and controls

34 lines (32 loc) · 1021 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package test;
public class MergeSort {
public void mergeSort(int[] array, int left, int right) {
int mid = (left + right) / 2;
if(left < right) {
mergeSort(array, left, mid);
mergeSort(array, mid + 1, right);
mergeTwoArray(array, left, mid, right);
}
}
private void mergeTwoArray(int[] array, int left, int mid, int right) {
int[] temp = new int[right - left + 1];
int i = left; // 左指针
int j = mid + 1; // 右指针
int count = 0;
// 把较小的数先移到新数组中
while (i <= mid && j <= right) {
if(array[i] <= array[j]) {
temp[count++] = array[i++];
} else {
temp[count++] = array[j++];
}
}
if(i <= mid) {
temp[count++] = array[i++];
}
if(j <= right) {
temp[count++] = array[j++];
}
System.arraycopy(temp, 0, array, left, temp.length);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.