Skip to content

Commit 3ccf53c

Browse files
committed
堆排序
1 parent 25bb164 commit 3ccf53c

2 files changed

Lines changed: 135 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package io.github.zebinh.algorithmJava.algorithmBook.sort;
2+
3+
4+
/**
5+
* 堆排序
6+
* @author zebinh
7+
* @date 2020年4月15日 22点13分
8+
*/
9+
public class HeapSortArray06 {
10+
11+
12+
/**
13+
* leetcode中的模板方法
14+
* @param nums
15+
* @return
16+
*/
17+
public static int[] sortArray(int[] nums) {
18+
int[] tmp = new int[nums.length+1];
19+
for (int i = 0; i < nums.length; i++){
20+
tmp[i+1] = nums[i];
21+
}
22+
23+
24+
for (int i = 1; i < tmp.length; i++) {
25+
swim(tmp, i);
26+
}
27+
for (int i = tmp.length-1; i >1; i--) {
28+
swap(tmp, 1, i);
29+
sink(tmp, i-1);
30+
}
31+
32+
// 搬运回去
33+
for (int i = 0; i < nums.length; i++){
34+
nums[i] = tmp[i+1];
35+
}
36+
return nums;
37+
}
38+
39+
40+
public static void swim(int[] nums, int i) {
41+
42+
while(i > 1 && nums[i] > nums[i/2]) {
43+
swap(nums, i, i/2);
44+
i /= 2;
45+
}
46+
}
47+
48+
public static void sink(int[] nums, int end) {
49+
int cur = 1;
50+
while (cur * 2 <= end) {
51+
int max = cur * 2;
52+
if (max+1 <= end && nums[max] < nums[max+1]) {
53+
max = max+1;
54+
}
55+
if (nums[max] < nums[cur]){
56+
break;
57+
}
58+
swap(nums, max, cur);
59+
cur = max;
60+
}
61+
}
62+
63+
/**
64+
* 交换两个数的工具方法
65+
* @param nums
66+
* @param a
67+
* @param b
68+
*/
69+
private static void swap(int[] nums, int a, int b) {
70+
int tmp = nums[a];
71+
nums[a] = nums[b];
72+
nums[b] = tmp;
73+
}
74+
75+
public static void main(String[] args) {
76+
// 测试用例
77+
int[] nums = new int[]{5, 2, 3, 1};
78+
sortArray(nums);
79+
// 打印结果
80+
for(int i = 0; i < nums.length; i++) {
81+
System.out.print(nums[i] + " ");
82+
}
83+
}
84+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.github.zebinh.algorithmJava.algorithmBook.sort;
2+
3+
4+
import java.util.PriorityQueue;
5+
6+
/**
7+
* 堆排序 - 使用Java的优先队列
8+
* @author zebinh
9+
* @date 2020年4月15日 22点13分
10+
*/
11+
public class HeapSortPriorityQueue06 {
12+
13+
14+
/**
15+
* leetcode中的模板方法
16+
* @param nums
17+
* @return
18+
*/
19+
public static int[] sortArray(int[] nums) {
20+
PriorityQueue<Integer> pq = new PriorityQueue<>(nums.length);
21+
for(int i = 0; i < nums.length; i++) {
22+
pq.add(nums[i]);
23+
}
24+
for (int i = 0; i < nums.length; i++) {
25+
nums[i] = pq.poll();
26+
}
27+
return nums;
28+
}
29+
30+
/**
31+
* 交换两个数的工具方法
32+
* @param nums
33+
* @param a
34+
* @param b
35+
*/
36+
private static void swap(int[] nums, int a, int b) {
37+
int tmp = nums[a];
38+
nums[a] = nums[b];
39+
nums[b] = tmp;
40+
}
41+
42+
public static void main(String[] args) {
43+
// 测试用例
44+
int[] nums = new int[]{5, 2, 3, 1};
45+
sortArray(nums);
46+
// 打印结果
47+
for(int i = 0; i < nums.length; i++) {
48+
System.out.print(nums[i] + " ");
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)