mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-31 11:05:36 +00:00
Update 算法 - 排序.md
堆的删除补充空堆检查并注明容量,快速选择先打乱数组。
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user