Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3eb418e

Browse files
committed
堆排序内容优化
1 parent ae3d672 commit 3eb418e

File tree

1 file changed

+150
-3
lines changed

1 file changed

+150
-3
lines changed

‎Contents/01.Array/02.Array-Sort/07.Array-Heap-Sort.md

Lines changed: 150 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class MaxHeap:
2626
self.max_heap = []
2727
```
2828

29-
![堆的存储结构](http://qcdn.itcharge.cn/images/20230824154601.png)
29+
![堆的存储结构](https://qcdn.itcharge.cn/images/20230824154601.png)
3030

3131
### 1.3 访问堆顶元素
3232

@@ -61,6 +61,38 @@ class MaxHeap:
6161

6262
这个过程称为「上移调整(Shift Up)」。因为新插入的元素会逐步向堆的上方移动,直到找到了合适的位置,保持堆的有序性。
6363

64+
::: tabs#heapPush
65+
66+
@tab <1>
67+
68+
![向堆中插入元素1](https://qcdn.itcharge.cn/images/20230831111022.png)
69+
70+
@tab <2>
71+
72+
![向堆中插入元素2](https://qcdn.itcharge.cn/images/20230831111036.png)
73+
74+
@tab <3>
75+
76+
![向堆中插入元素3](https://qcdn.itcharge.cn/images/20230831111052.png)
77+
78+
@tab <4>
79+
80+
![向堆中插入元素4](https://qcdn.itcharge.cn/images/20230831111103.png)
81+
82+
@tab <5>
83+
84+
![向堆中插入元素5](https://qcdn.itcharge.cn/images/20230831112321.png)
85+
86+
@tab <6>
87+
88+
![向堆中插入元素6](https://qcdn.itcharge.cn/images/20230831112328.png)
89+
90+
@tab <7>
91+
92+
![向堆中插入元素7](https://qcdn.itcharge.cn/images/20230831134124.png)
93+
94+
:::
95+
6496
```python
6597
class MaxHeap:
6698
......
@@ -78,7 +110,7 @@ class MaxHeap:
78110
i = (i - 1) // 2
79111
```
80112

81-
在最坏情况下,向堆中插入元素的时间复杂度为 $O(\log n),ドル其中 $n$ 是堆中元素的数量,这是因为堆的高度是 $\log n$。
113+
在最坏情况下,「向堆中插入元素」的时间复杂度为 $O(\log n),ドル其中 $n$ 是堆中元素的数量,这是因为堆的高度是 $\log n$。
82114

83115
### 1.5 删除堆顶元素
84116

@@ -95,6 +127,38 @@ class MaxHeap:
95127

96128
这个过程称为「下移调整(Shift Down)」。因为新的堆顶元素会逐步向堆的下方移动,直到找到了合适的位置,保持堆的有序性。
97129

130+
::: tabs#heapPop
131+
132+
@tab <1>
133+
134+
![删除堆顶元素 1](https://qcdn.itcharge.cn/images/20230831134148.png)
135+
136+
@tab <2>
137+
138+
![删除堆顶元素 2](https://qcdn.itcharge.cn/images/20230831134156.png)
139+
140+
@tab <3>
141+
142+
![删除堆顶元素 3](https://qcdn.itcharge.cn/images/20230831134205.png)
143+
144+
@tab <4>
145+
146+
![删除堆顶元素 4](https://qcdn.itcharge.cn/images/20230831134214.png)
147+
148+
@tab <5>
149+
150+
![删除堆顶元素 5](https://qcdn.itcharge.cn/images/20230831134221.png)
151+
152+
@tab <6>
153+
154+
![删除堆顶元素 6](https://qcdn.itcharge.cn/images/20230831134229.png)
155+
156+
@tab <7>
157+
158+
![删除堆顶元素 7](https://qcdn.itcharge.cn/images/20230831134237.png)
159+
160+
:::
161+
98162
```python
99163
class MaxHeap:
100164
......
@@ -143,7 +207,7 @@ class MaxHeap:
143207
break
144208
```
145209

146-
删除堆顶元素的时间复杂度通常为$O(\log n),ドル其中 $n$ 是堆中元素的数量,因为堆的高度是 $\log n$。
210+
「删除堆顶元素」的时间复杂度通常为$O(\log n),ドル其中 $n$ 是堆中元素的数量,因为堆的高度是 $\log n$。
147211

148212
## 2. 堆排序
149213

@@ -166,6 +230,89 @@ class MaxHeap:
166230
3. **重复交换和调整堆**:
167231
1. 重复第 2ドル$ 步,直到堆的大小为 1ドル$ 时,此时大顶堆的数组已经完全有序。
168232

233+
::: tabs#heapSortBuildMaxHeap
234+
235+
@tab <1>
236+
237+
![1. 构建初始大顶堆 1](https://qcdn.itcharge.cn/images/20230831151620.png)
238+
239+
@tab <2>
240+
241+
![1. 构建初始大顶堆 2](https://qcdn.itcharge.cn/images/20230831151641.png)
242+
243+
@tab <3>
244+
245+
![1. 构建初始大顶堆 3](https://qcdn.itcharge.cn/images/20230831151703.png)
246+
247+
@tab <4>
248+
249+
![1. 构建初始大顶堆 4](https://qcdn.itcharge.cn/images/20230831151715.png)
250+
251+
@tab <5>
252+
253+
![1. 构建初始大顶堆 5](https://qcdn.itcharge.cn/images/20230831151725.png)
254+
255+
@tab <6>
256+
257+
![1. 构建初始大顶堆 6](https://qcdn.itcharge.cn/images/20230831151735.png)
258+
259+
@tab <7>
260+
261+
![1. 构建初始大顶堆 7](https://qcdn.itcharge.cn/images/20230831151749.png)
262+
263+
:::
264+
265+
::: tabs#heapSortExchangeVal
266+
267+
@tab <1>
268+
269+
![2. 交换元素,调整堆 1](https://qcdn.itcharge.cn/images/20230831162335.png)
270+
271+
@tab <2>
272+
273+
![2. 交换元素,调整堆 2](https://qcdn.itcharge.cn/images/20230831162346.png)
274+
275+
@tab <3>
276+
277+
![2. 交换元素,调整堆 3](https://qcdn.itcharge.cn/images/20230831162359.png)
278+
279+
@tab <4>
280+
281+
![2. 交换元素,调整堆 4](https://qcdn.itcharge.cn/images/20230831162408.png)
282+
283+
@tab <5>
284+
285+
![2. 交换元素,调整堆 5](https://qcdn.itcharge.cn/images/20230831162416.png)
286+
287+
@tab <6>
288+
289+
![2. 交换元素,调整堆 6](https://qcdn.itcharge.cn/images/20230831162424.png)
290+
291+
@tab <7>
292+
293+
![2. 交换元素,调整堆 7](https://qcdn.itcharge.cn/images/20230831162431.png)
294+
295+
@tab <8>
296+
297+
![2. 交换元素,调整堆 8](https://qcdn.itcharge.cn/images/20230831162440.png)
298+
299+
@tab <9>
300+
301+
![2. 交换元素,调整堆 9](https://qcdn.itcharge.cn/images/20230831162449.png)
302+
303+
@tab <10>
304+
305+
![2. 交换元素,调整堆 10](https://qcdn.itcharge.cn/images/20230831162457.png)
306+
307+
@tab <11>
308+
309+
![https://qcdn.](https://qcdn.itcharge.cn/images/20230831162505.png)
310+
311+
@tab <12>
312+
313+
![2. 交换元素,调整堆 12](https://qcdn.itcharge.cn/images/20230831162512.png)
314+
315+
:::
169316

170317
### 2.3 堆排序代码实现
171318

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /