-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
50 lines (47 loc) · 1.26 KB
/
Copy pathMergeSort.java
File metadata and controls
50 lines (47 loc) · 1.26 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
package com.sort;
/**
* @author cx
* @date 2018/8/27 15:16
* @description 归并排序 全为O(nlogn) 分治 ,分成子问题,再合并
*/
public class MergeSort {
public static void sort(int arr[]){
int[] temp =new int[arr.length];
sort(arr,0,arr.length-1,temp);
}
public static void sort(int arr[],int start,int end,int[] temp){
if (start<end){
int mid = (start+end)/2;
sort(arr,start,mid,temp);
sort(arr,mid+1,end,temp);
merge(arr,start,mid,end,temp);
}
}
public static void merge(int arr[],int start,int mid,int end ,int[] temp){
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<=mid){
temp[k++]=arr[j++];
}
k=0;
while (start<=end){
arr[start++]=temp[k++];
}
}
public static void main(String[] args) {
int arr[]={9,8,7,6,5,4,3,2,1};
sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}