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
41 lines (37 loc) · 1 KB

File metadata and controls

41 lines (37 loc) · 1 KB
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
35
36
37
38
39
40
41
package sort;
/**
* 自顶向下归并排序
*
* @author 刘壮飞
* https://github.com/zfman.
* https://blog.csdn.net/lzhuangfei.
*/
public class UpToDownMergeSort<T extends Comparable<T>> extends MergeSort<T> {
/**
* 因为每次都将问题对半分成两个子问题,
* 而这种对半分的算法复杂度一般为 O(NlogN),
* 因此该归并排序方法的时间复杂度也为 O(NlogN)。
*
* @param nums
*/
@Override
public void sort(T[] nums) {
tmp= (T[]) new Comparable[nums.length];
sort(nums,0,nums.length-1);
}
public void sort(T[] nums,int l,int h) {
if(l>=h) return;
int mid=l+(h-l)/2;
sort(nums,l,mid);
sort(nums,mid+1,h);
merge(nums,l,mid,h);
}
public static void main(String[] args){
Integer[] arr={
5,1,8,7,10,6,9,5,20,3,0
};
Sort<Integer> sort=new UpToDownMergeSort<>();
sort.sort(arr);
ArrayUtils.printArray(arr);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.