堆排序

堆排序


“The Linux philosophy is “Laugh in the face of danger”.Oops.Wrong One. “Do it yourself”. Yes, that”s it.”
Linux的哲学就是“在危险面前放声大笑”,呵呵,不是这句,应该是“一切靠自己,自力更生”才对。

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
public class HeapSort {
public static void HeapSort (int[] nums) {
buildHeap(nums);
for(int i=nums.length-1;i>=0;i--){
int temp = nums[0];
nums[0] = nums[i];
nums[i] = temp;
maxHeapfy(nums,0,i);
}
}

/**
* 建堆,从下往上调整,从(n-1)/2-0,只有这些节点有子节点
*/
private static void buildHeap (int[] nums) {
for (int i = ((nums.length - 1) - 1) / 2; i >= 0; --i) {
maxHeapfy(nums,i,nums.length);
}
}

//length排序时用来限定排好的位数
private static void maxHeapfy (int[] nums, int index,int length) {
for (; ; ) {
int max = index;
int left = 2 * max + 1;
int right = 2 * max + 2;
//父节点、左孩子、右孩子选出最大
if (left < length && nums[left] > nums[max])
max = left;
if (right < length && nums[right] > nums[max])
max = right;
//交换并递归调整
if (max != index) {
int temp = nums[index];
nums[index] = nums[max];
nums[max] = temp;
index = max;
} else
break;
}
}
public static void main (String[] args) {
int []nums = new int[] {4,3,6,2,1,8,5,9,10,0};
HeapSort(nums);
for(int t:nums) {
System.out.print(t+" ");
}
}
}
Thanks!