-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeSort.java
More file actions
71 lines (61 loc) · 1.74 KB
/
mergeSort.java
File metadata and controls
71 lines (61 loc) · 1.74 KB
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
61
62
63
64
65
66
67
68
69
70
71
import javax.sound.midi.MidiChannel;
/**
*
*/
/**
* @author DELL
* Project Name: Java_learn
* Class Name: mergeSort
* date: 2019年9月10日
*/
public class mergeSort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] lyst = {23, 46, 0, 8, 11, 18};
System.out.println("before sorted: ");
for (int i = 0; i < lyst.length; i++) {
System.out.print(lyst[i] + " ");
}
// 归并排序包
// Arrays.parallelSort(lyst);
System.out.println();
merge_sort(lyst, 0 , lyst.length - 1);
System.out.println("After sorted: ");
for (int i = 0; i < lyst.length; i++) {
System.out.print(lyst[i] + " ");
}
}
public static void merge_sort(int[] lyst, int low, int high) {
if(low < high) {
int mid = (low + high) / 2;
merge_sort(lyst, low, mid);
merge_sort(lyst, mid+1, high);
merge(lyst, low, mid, high);
}
}
public static void merge(int[] lyst, int low, int mid, int high) {
int[] container = new int[high - low + 1];
int i = low;
int j = mid+1;
int k = 0;
while(i <= mid && j <= high) {
if(lyst[i] < lyst[j]) {
container[k++] = lyst[i++];
} else {
container[k++] = lyst[j++];
}
}
while(i <= mid) {
container[k++] = lyst[i++];
}
while(j <= high) {
container[k++] = lyst[j++];
}
for(int k2=0; k2 < container.length; k2++) {
lyst[low+k2] = container[k2];
}
}
}