From b931e0783dbedff50ddf18014c8aba7fd7f661a3 Mon Sep 17 00:00:00 2001 From: CyC2018 Date: Sat, 31 Oct 2026 14:03:54 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E7=AE=97=E6=B3=95=20-=20=E6=8E=92?= =?UTF-8?q?=E5=BA=8F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 堆的删除补充空堆检查并注明容量,快速选择先打乱数组。 --- notes/算法 - 排序.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/notes/算法 - 排序.md b/notes/算法 - 排序.md index da0d046e..ed678c5a 100644 --- a/notes/算法 - 排序.md +++ b/notes/算法 - 排序.md @@ -363,6 +363,7 @@ public class ThreeWayQuickSort> extends QuickSort { ```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> { 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;