漫画:什么是堆排序?
漫画算法:无序数组排序后的最大相邻差值
1 堆排序
1.1 原理:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9d10c6a3552c2ee32144e1176671dda5.png)
1.2 实现:
import java.util.Arrays;
public class HeapSort {
public static void main(String[] args) {
int[] ints = {99, 100, -30, 0, 99, 2, -2, -15, 2, 2, 333, 666, 999, 27, 22, 13, 8, 7, 2, -30};
System.out.println("排序前:" + Arrays.toString(ints));
System.out.println("排序后:" + Arrays.toString(heapSort(ints)));
}
private static int[] heapSort(int[] ints) {
if (ints == null || ints.length <= 1) {
return ints;
}
buildHeap(ints);
for (int i = ints.length - 1; i >= 1; i--) {
swap(ints, 0, i);
adjustHeap(ints, 0, i - 1);
}
return ints;
}
private static void buildHeap(int[] ints) {
for (int i = (ints.length - 1 - 1) / 2; i >= 0; i--) {
adjustHeap(ints, i, ints.length - 1);
}
}
private static void adjustHeap(int[] ints, int from, int to) {
int maxChild = from * 2 + 1;
if (maxChild > to) {
return;
}
int rightChild = maxChild + 1;
if (rightChild <= to && ints[rightChild] > ints[maxChild]) {
maxChild = rightChild;
}
if (ints[maxChild] > ints[from]) {
swap(ints, maxChild, from);
}
adjustHeap(ints, maxChild, to);
}
private static void swap(int[] ints, int index1, int index2) {
int temp = ints[index1];
ints[index1] = ints[index2];
ints[index2] = temp;
}
}
1.3 测试:
D:\program\Java\jdk1.8.0_241\bin\java.exe -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:62282,suspend=y,server=n "-javaagent:C:\Users\GMWANG~1\AppData\Local\Temp\captureAgent163jars\debugger-agent.jar" -Dfile.encoding=UTF-8 -classpath "D:\program\Java\jdk1.8.0_241\jre\lib\charsets.jar;D:\program\Java\jdk1.8.0_241\jre\lib\deploy.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\access-bridge-64.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\cldrdata.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\dnsns.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\jaccess.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\jfxrt.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\localedata.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\nashorn.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\sunec.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\sunjce_provider.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\sunmscapi.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\sunpkcs11.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\zipfs.jar;D:\program\Java\jdk1.8.0_241\jre\lib\javaws.jar;D:\program\Java\jdk1.8.0_241\jre\lib\jce.jar;D:\program\Java\jdk1.8.0_241\jre\lib\jfr.jar;D:\program\Java\jdk1.8.0_241\jre\lib\jfxswt.jar;D:\program\Java\jdk1.8.0_241\jre\lib\jsse.jar;D:\program\Java\jdk1.8.0_241\jre\lib\management-agent.jar;D:\program\Java\jdk1.8.0_241\jre\lib\plugin.jar;D:\program\Java\jdk1.8.0_241\jre\lib\resources.jar;D:\program\Java\jdk1.8.0_241\jre\lib\rt.jar;D:\project\untitled\out\production\untitled;D:\program\JetBrains\IntelliJ IDEA 2020.1\lib\idea_rt.jar" HeapSort
Connected to the target VM, address: '127.0.0.1:62282', transport: 'socket'
排序前:[99, 100, -30, 0, 99, 2, -2, -15, 2, 2, 333, 666, 999, 27, 22, 13, 8, 7, 2, -30]
排序后:[-30, -30, -15, -2, 0, 2, 2, 2, 2, 7, 8, 13, 22, 27, 99, 99, 100, 333, 666, 999]
Disconnected from the target VM, address: '127.0.0.1:62282', transport: 'socket'
Process finished with exit code 0
2 快速排序与堆排序的比较
| 快速排序 | 堆排序 |
---|
最坏时间复杂度 | O(n2) | O(n logn) |
空间复杂度 | O(1) | O(logn) |