auto commit
Before Width: | Height: | Size: 29 KiB After Width: | Height: | Size: 16 KiB |
BIN
docs/notes/pics/101550406418006.gif
Normal file
After Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 39 KiB |
BIN
docs/notes/pics/111550407277293.gif
Normal file
After Width: | Height: | Size: 39 KiB |
BIN
docs/notes/pics/11_200.png
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
docs/notes/pics/121550407878282.gif
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
docs/notes/pics/81550405360028.gif
Normal file
After Width: | Height: | Size: 8.0 KiB |
BIN
docs/notes/pics/8_200.png
Normal file
After Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 8.0 KiB |
BIN
docs/notes/pics/91550405374894.gif
Normal file
After Width: | Height: | Size: 8.0 KiB |
BIN
docs/notes/pics/9_200.png
Normal file
After Width: | Height: | Size: 23 KiB |
@ -646,7 +646,7 @@ public T select(T[] nums, int k) {
|
||||
|
||||
堆可以用数组来表示,这是因为堆是完全二叉树,而完全二叉树很容易就存储在数组中。位置 k 的节点的父节点位置为 k/2,而它的两个子节点的位置分别为 2k 和 2k+1。这里不使用数组索引为 0 的位置,是为了更清晰地描述节点的位置关系。
|
||||
|
||||
<div align="center"> <img src="pics/f3080f83-6239-459b-8e9c-03b6641f7815.png" width="200"/> </div><br>
|
||||
<div align="center"> <img src="pics/8_200.png"/> </div><br>
|
||||
|
||||
```java
|
||||
public class Heap<T extends Comparable<T>> {
|
||||
@ -682,7 +682,7 @@ public class Heap<T extends Comparable<T>> {
|
||||
|
||||
在堆中,当一个节点比父节点大,那么需要交换这个两个节点。交换后还可能比它新的父节点大,因此需要不断地进行比较和交换操作,把这种操作称为上浮。
|
||||
|
||||
<div align="center"> <img src="pics/33ac2b23-cb85-4e99-bc41-b7b7199fad1c.png" width="400"/> </div><br>
|
||||
<div align="center"> <img src="pics/81550405360028.gif"/> </div><br>
|
||||
|
||||
```java
|
||||
private void swim(int k) {
|
||||
@ -695,7 +695,7 @@ private void swim(int k) {
|
||||
|
||||
类似地,当一个节点比子节点来得小,也需要不断地向下进行比较和交换操作,把这种操作称为下沉。一个节点如果有两个子节点,应当与两个子节点中最大那个节点进行交换。
|
||||
|
||||
<div align="center"> <img src="pics/72f0ff69-138d-4e54-b7ac-ebe025d978dc.png" width="400"/> </div><br>
|
||||
<div align="center"> <img src="pics/91550405374894.gif"/> </div><br>
|
||||
|
||||
```java
|
||||
private void sink(int k) {
|
||||
@ -744,15 +744,13 @@ public T delMax() {
|
||||
|
||||
无序数组建立堆最直接的方法是从左到右遍历数组进行上浮操作。一个更高效的方法是从右至左进行下沉操作,如果一个节点的两个节点都已经是堆有序,那么进行下沉操作可以使得这个节点为根节点的堆有序。叶子节点不需要进行下沉操作,可以忽略叶子节点的元素,因此只需要遍历一半的元素即可。
|
||||
|
||||
<div align="center"> <img src="pics/b84ba6fb-312b-4e69-8c77-fb6eb6fb38d4.png" width="300"/> </div><br>
|
||||
<div align="center"> <img src="pics/101550406418006.gif"/> </div><br>
|
||||
|
||||
#### 5.2 交换堆顶元素与最后一个元素
|
||||
|
||||
交换之后需要进行下沉操作维持堆的有序状态。
|
||||
|
||||
<div align="center"> <img src="pics/51fb761d-8ce0-4472-92ff-2f227ac7888a.png" width="270"/> </div><br>
|
||||
|
||||
<div align="center"> <img src="pics/b20a3466-44b4-445e-87c7-dd4fb9ef44b2.png" width="350"/> </div><br>
|
||||
<div align="center"> <img src="pics/111550407277293.gif"/> </div><br>
|
||||
|
||||
```java
|
||||
public class HeapSort<T extends Comparable<T>> extends Sort<T> {
|
||||
@ -2045,7 +2043,7 @@ public class Transaction {
|
||||
|
||||
对于 N 个键,M 条链表 (N>M),如果哈希函数能够满足均匀性的条件,每条链表的大小趋向于 N/M,因此未命中的查找和插入操作所需要的比较次数为 \~N/M。
|
||||
|
||||
<div align="center"> <img src="pics/b4252c85-6fb0-4995-9a68-a1a5925fbdb1.png" width="300"/> </div><br>
|
||||
<div align="center"> <img src="pics/9_200.png"/> </div><br>
|
||||
|
||||
### 3. 线性探测法
|
||||
|
||||
@ -2053,7 +2051,7 @@ public class Transaction {
|
||||
|
||||
使用线性探测法,数组的大小 M 应当大于键的个数 N(M>N)。
|
||||
|
||||
<div align="center"> <img src="pics/dbb8516d-37ba-4e2c-b26b-eefd7de21b45.png" width="400"/> </div><br>
|
||||
<div align="center"> <img src="pics/121550407878282.gif"/> </div><br>
|
||||
|
||||
```java
|
||||
public class LinearProbingHashST<Key, Value> implements UnorderedST<Key, Value> {
|
||||
@ -2154,7 +2152,7 @@ public void delete(Key key) {
|
||||
|
||||
线性探测法的成本取决于连续条目的长度,连续条目也叫聚簇。当聚簇很长时,在查找和插入时也需要进行很多次探测。例如下图中 2\~5 位置就是一个聚簇。
|
||||
|
||||
<div align="center"> <img src="pics/386cd64f-7a9d-40e6-8c55-22b90ee2d258.png" width="400"/> </div><br>
|
||||
<div align="center"> <img src="pics/11_200.png"/> </div><br>
|
||||
|
||||
α = N/M,把 α 称为使用率。理论证明,当 α 小于 1/2 时探测的预计次数只在 1.5 到 2.5 之间。为了保证散列表的性能,应当调整数组的大小,使得 α 在 [1/4, 1/2] 之间。
|
||||
|
||||
|
BIN
docs/pics/10.gif
Before Width: | Height: | Size: 29 KiB After Width: | Height: | Size: 16 KiB |
BIN
docs/pics/101550406418006.gif
Normal file
After Width: | Height: | Size: 16 KiB |
BIN
docs/pics/11.gif
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 39 KiB |
BIN
docs/pics/111550407277293.gif
Normal file
After Width: | Height: | Size: 39 KiB |
BIN
docs/pics/11_200.png
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
docs/pics/121550407878282.gif
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
docs/pics/81550405360028.gif
Normal file
After Width: | Height: | Size: 8.0 KiB |
BIN
docs/pics/8_200.png
Normal file
After Width: | Height: | Size: 15 KiB |
BIN
docs/pics/9.gif
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 8.0 KiB |
BIN
docs/pics/91550405374894.gif
Normal file
After Width: | Height: | Size: 8.0 KiB |
BIN
docs/pics/9_200.png
Normal file
After Width: | Height: | Size: 23 KiB |