Update 算法 - 排序.md

堆的删除补充空堆检查并注明容量,快速选择先打乱数组。
This commit is contained in:
CyC2018
2026-10-31 14:03:54 +08:00
parent 488219be0b
commit b931e0783d
+4
View File
@@ -363,6 +363,7 @@ public class ThreeWayQuickSort<T extends Comparable<T>> extends QuickSort<T> {
```java
public T select(T[] nums, int k) {
shuffle(nums);
int l = 0, h = nums.length - 1;
while (h > l) {
int j = partition(nums, l, h);
@@ -397,6 +398,7 @@ public class Heap<T extends Comparable<T>> {
private T[] heap;
private int N = 0;
// 容量固定为 maxN,插入超过 maxN 个元素时数组越界
public Heap(int maxN) {
this.heap = (T[]) new Comparable[maxN + 1];
}
@@ -471,6 +473,8 @@ public void insert(T v) {
```java
public T delMax() {
if (isEmpty())
throw new NoSuchElementException("堆为空");
T max = heap[1];
swap(1, N--);
heap[N + 1] = null;