diff --git a/codes/dart/chapter_heap/my_heap.dart b/codes/dart/chapter_heap/my_heap.dart index 854e6fca2..f45da8994 100644 --- a/codes/dart/chapter_heap/my_heap.dart +++ b/codes/dart/chapter_heap/my_heap.dart @@ -36,11 +36,9 @@ class MaxHeap { /* 交换元素 */ void _swap(int i, int j) { - int a = _maxHeap[i]; - int b = _maxHeap[j]; - int tem = a; - _maxHeap[i] = b; - _maxHeap[j] = tem; + int tmp = _maxHeap[i]; + _maxHeap[i] = _maxHeap[j];; + _maxHeap[j] = tmp; } /* 获取堆大小 */ diff --git a/codes/java/chapter_heap/my_heap.java b/codes/java/chapter_heap/my_heap.java index f136dbb85..d9859bb56 100644 --- a/codes/java/chapter_heap/my_heap.java +++ b/codes/java/chapter_heap/my_heap.java @@ -41,10 +41,8 @@ class MaxHeap { /* 交换元素 */ private void swap(int i, int j) { - int a = maxHeap.get(i); - int b = maxHeap.get(j); - int tmp = a; - maxHeap.set(i, b); + int tmp = maxHeap.get(i); + maxHeap.set(i, maxHeap.get(j)); maxHeap.set(j, tmp); } diff --git a/codes/javascript/chapter_heap/my_heap.js b/codes/javascript/chapter_heap/my_heap.js index d48471a58..fefb8e066 100644 --- a/codes/javascript/chapter_heap/my_heap.js +++ b/codes/javascript/chapter_heap/my_heap.js @@ -37,10 +37,8 @@ class MaxHeap { /* 交换元素 */ #swap(i, j) { - const a = this.#maxHeap[i], - b = this.#maxHeap[j], - tmp = a; - this.#maxHeap[i] = b; + const tmp = this.#maxHeap[i]; + this.#maxHeap[i] = this.#maxHeap[j]; this.#maxHeap[j] = tmp; } diff --git a/codes/python/chapter_heap/my_heap.py b/codes/python/chapter_heap/my_heap.py index 08ce0a6a2..f66abb78c 100644 --- a/codes/python/chapter_heap/my_heap.py +++ b/codes/python/chapter_heap/my_heap.py @@ -35,8 +35,7 @@ class MaxHeap: def swap(self, i: int, j: int): """交换元素""" - a, b = self.max_heap[i], self.max_heap[j] - self.max_heap[i], self.max_heap[j] = b, a + self.max_heap[i], self.max_heap[j] = self.max_heap[j], self.max_heap[i] def size(self) -> int: """获取堆大小""" diff --git a/codes/typescript/chapter_heap/my_heap.ts b/codes/typescript/chapter_heap/my_heap.ts index 51d0bfa06..72d95541e 100644 --- a/codes/typescript/chapter_heap/my_heap.ts +++ b/codes/typescript/chapter_heap/my_heap.ts @@ -36,10 +36,8 @@ class MaxHeap { /* 交换元素 */ private swap(i: number, j: number): void { - const a = this.maxHeap[i], - b = this.maxHeap[j], - tmp = a; - this.maxHeap[i] = b; + const tmp = this.maxHeap[i]; + this.maxHeap[i] = this.maxHeap[j]; this.maxHeap[j] = tmp; } diff --git a/codes/zig/chapter_heap/my_heap.zig b/codes/zig/chapter_heap/my_heap.zig index ce5f694ef..8aa7d1cc7 100644 --- a/codes/zig/chapter_heap/my_heap.zig +++ b/codes/zig/chapter_heap/my_heap.zig @@ -48,10 +48,8 @@ pub fn MaxHeap(comptime T: type) type { // 交换元素 fn swap(self: *Self, i: usize, j: usize) !void { - var a = self.max_heap.?.items[i]; - var b = self.max_heap.?.items[j]; - var tmp = a; - try self.max_heap.?.replaceRange(i, 1, &[_]T{b}); + var tmp = self.max_heap.?.items[i]; + try self.max_heap.?.replaceRange(i, 1, &[_]T{self.max_heap.?.items[j]}); try self.max_heap.?.replaceRange(j, 1, &[_]T{tmp}); } diff --git a/docs/chapter_heap/build_heap.assets/heapify_operations_count.png b/docs/chapter_heap/build_heap.assets/heapify_operations_count.png index 079c53e1a..83f41cd76 100644 Binary files a/docs/chapter_heap/build_heap.assets/heapify_operations_count.png and b/docs/chapter_heap/build_heap.assets/heapify_operations_count.png differ diff --git a/docs/chapter_heap/heap.assets/heap_pop_step1.png b/docs/chapter_heap/heap.assets/heap_pop_step1.png index d8593777d..97498a8aa 100644 Binary files a/docs/chapter_heap/heap.assets/heap_pop_step1.png and b/docs/chapter_heap/heap.assets/heap_pop_step1.png differ diff --git a/docs/chapter_heap/heap.assets/heap_pop_step10.png b/docs/chapter_heap/heap.assets/heap_pop_step10.png index a71bc29c8..7465423dd 100644 Binary files a/docs/chapter_heap/heap.assets/heap_pop_step10.png and b/docs/chapter_heap/heap.assets/heap_pop_step10.png differ diff --git a/docs/chapter_heap/heap.assets/heap_pop_step2.png b/docs/chapter_heap/heap.assets/heap_pop_step2.png index 0ae3f031a..e84b6d5c6 100644 Binary files a/docs/chapter_heap/heap.assets/heap_pop_step2.png and b/docs/chapter_heap/heap.assets/heap_pop_step2.png differ diff --git a/docs/chapter_heap/heap.assets/heap_pop_step3.png b/docs/chapter_heap/heap.assets/heap_pop_step3.png index f62f74fc9..c2f23e958 100644 Binary files a/docs/chapter_heap/heap.assets/heap_pop_step3.png and b/docs/chapter_heap/heap.assets/heap_pop_step3.png differ diff --git a/docs/chapter_heap/heap.assets/heap_pop_step4.png b/docs/chapter_heap/heap.assets/heap_pop_step4.png index 459a4522f..9180d44f2 100644 Binary files a/docs/chapter_heap/heap.assets/heap_pop_step4.png and b/docs/chapter_heap/heap.assets/heap_pop_step4.png differ diff --git a/docs/chapter_heap/heap.assets/heap_pop_step5.png b/docs/chapter_heap/heap.assets/heap_pop_step5.png index ecad35674..0c560a3d4 100644 Binary files a/docs/chapter_heap/heap.assets/heap_pop_step5.png and b/docs/chapter_heap/heap.assets/heap_pop_step5.png differ diff --git a/docs/chapter_heap/heap.assets/heap_pop_step6.png b/docs/chapter_heap/heap.assets/heap_pop_step6.png index 33a00cf50..5f03048ee 100644 Binary files a/docs/chapter_heap/heap.assets/heap_pop_step6.png and b/docs/chapter_heap/heap.assets/heap_pop_step6.png differ diff --git a/docs/chapter_heap/heap.assets/heap_pop_step7.png b/docs/chapter_heap/heap.assets/heap_pop_step7.png index 228e80194..0229fe9f6 100644 Binary files a/docs/chapter_heap/heap.assets/heap_pop_step7.png and b/docs/chapter_heap/heap.assets/heap_pop_step7.png differ diff --git a/docs/chapter_heap/heap.assets/heap_pop_step8.png b/docs/chapter_heap/heap.assets/heap_pop_step8.png index 060984342..daea0f265 100644 Binary files a/docs/chapter_heap/heap.assets/heap_pop_step8.png and b/docs/chapter_heap/heap.assets/heap_pop_step8.png differ diff --git a/docs/chapter_heap/heap.assets/heap_pop_step9.png b/docs/chapter_heap/heap.assets/heap_pop_step9.png index 0672abe36..714a3c7e0 100644 Binary files a/docs/chapter_heap/heap.assets/heap_pop_step9.png and b/docs/chapter_heap/heap.assets/heap_pop_step9.png differ diff --git a/docs/chapter_heap/heap.assets/heap_push_step1.png b/docs/chapter_heap/heap.assets/heap_push_step1.png index cb0466413..c94ab8ae3 100644 Binary files a/docs/chapter_heap/heap.assets/heap_push_step1.png and b/docs/chapter_heap/heap.assets/heap_push_step1.png differ diff --git a/docs/chapter_heap/heap.assets/heap_push_step2.png b/docs/chapter_heap/heap.assets/heap_push_step2.png index 49d6765b9..6cd132ddc 100644 Binary files a/docs/chapter_heap/heap.assets/heap_push_step2.png and b/docs/chapter_heap/heap.assets/heap_push_step2.png differ diff --git a/docs/chapter_heap/heap.assets/heap_push_step3.png b/docs/chapter_heap/heap.assets/heap_push_step3.png index addd2748f..94931fa95 100644 Binary files a/docs/chapter_heap/heap.assets/heap_push_step3.png and b/docs/chapter_heap/heap.assets/heap_push_step3.png differ diff --git a/docs/chapter_heap/heap.assets/heap_push_step4.png b/docs/chapter_heap/heap.assets/heap_push_step4.png index 69eac84da..54c757795 100644 Binary files a/docs/chapter_heap/heap.assets/heap_push_step4.png and b/docs/chapter_heap/heap.assets/heap_push_step4.png differ diff --git a/docs/chapter_heap/heap.assets/heap_push_step5.png b/docs/chapter_heap/heap.assets/heap_push_step5.png index 1ed9dd42c..a813bca63 100644 Binary files a/docs/chapter_heap/heap.assets/heap_push_step5.png and b/docs/chapter_heap/heap.assets/heap_push_step5.png differ diff --git a/docs/chapter_heap/heap.assets/heap_push_step6.png b/docs/chapter_heap/heap.assets/heap_push_step6.png index 1a9a7ae33..3fe1e3aed 100644 Binary files a/docs/chapter_heap/heap.assets/heap_push_step6.png and b/docs/chapter_heap/heap.assets/heap_push_step6.png differ diff --git a/docs/chapter_heap/heap.assets/heap_push_step7.png b/docs/chapter_heap/heap.assets/heap_push_step7.png new file mode 100644 index 000000000..3ce0f4bfc Binary files /dev/null and b/docs/chapter_heap/heap.assets/heap_push_step7.png differ diff --git a/docs/chapter_heap/heap.assets/heap_push_step8.png b/docs/chapter_heap/heap.assets/heap_push_step8.png new file mode 100644 index 000000000..1c120bfe0 Binary files /dev/null and b/docs/chapter_heap/heap.assets/heap_push_step8.png differ diff --git a/docs/chapter_heap/heap.assets/heap_push_step9.png b/docs/chapter_heap/heap.assets/heap_push_step9.png new file mode 100644 index 000000000..8b8c6c0d5 Binary files /dev/null and b/docs/chapter_heap/heap.assets/heap_push_step9.png differ diff --git a/docs/chapter_heap/heap.assets/min_heap_and_max_heap.png b/docs/chapter_heap/heap.assets/min_heap_and_max_heap.png index 435ce7f2c..719ebd9e1 100644 Binary files a/docs/chapter_heap/heap.assets/min_heap_and_max_heap.png and b/docs/chapter_heap/heap.assets/min_heap_and_max_heap.png differ diff --git a/docs/chapter_heap/heap.assets/representation_of_heap.png b/docs/chapter_heap/heap.assets/representation_of_heap.png index a267d24d0..824b8e9a3 100644 Binary files a/docs/chapter_heap/heap.assets/representation_of_heap.png and b/docs/chapter_heap/heap.assets/representation_of_heap.png differ diff --git a/docs/chapter_heap/heap.md b/docs/chapter_heap/heap.md index 1066f1aff..23f18d885 100644 --- a/docs/chapter_heap/heap.md +++ b/docs/chapter_heap/heap.md @@ -505,6 +505,15 @@ === "<6>" ![heap_push_step6](heap.assets/heap_push_step6.png) +=== "<7>" + ![heap_push_step7](heap.assets/heap_push_step7.png) + +=== "<8>" + ![heap_push_step8](heap.assets/heap_push_step8.png) + +=== "<9>" + ![heap_push_step9](heap.assets/heap_push_step9.png) + 设节点总数为 $n$ ,则树的高度为 $O(\log n)$ 。由此可知,堆化操作的循环轮数最多为 $O(\log n)$ ,**元素入堆操作的时间复杂度为 $O(\log n)$** 。 === "Java" @@ -712,5 +721,5 @@ ## 堆常见应用 - **优先队列**:堆通常作为实现优先队列的首选数据结构,其入队和出队操作的时间复杂度均为 $O(\log n)$ ,而建队操作为 $O(n)$ ,这些操作都非常高效。 -- **堆排序**:给定一组数据,我们可以用它们建立一个堆,然后不断地执行元素出堆操作,从而得到有序数据。当然,堆排序还有一种更优雅的实现,详见后续的堆排序章节。 +- **堆排序**:给定一组数据,我们可以用它们建立一个堆,然后不断地执行元素出堆操作,从而得到有序数据。然而,我们通常会使用一种更优雅的方式实现堆排序,详见后续的堆排序章节。 - **获取最大的 $k$ 个元素**:这是一个经典的算法问题,同时也是一种典型应用,例如选择热度前 10 的新闻作为微博热搜,选取销量前 10 的商品等。 diff --git a/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step1.png b/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step1.png index 40a6cff7f..71efbc98f 100644 Binary files a/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step1.png and b/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step1.png differ diff --git a/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step2.png b/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step2.png index ad6f69f18..d02d33ad2 100644 Binary files a/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step2.png and b/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step2.png differ diff --git a/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step3.png b/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step3.png index d8b521d18..fd5400fdd 100644 Binary files a/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step3.png and b/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step3.png differ diff --git a/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step4.png b/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step4.png index 31865cbe7..1a61cf997 100644 Binary files a/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step4.png and b/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step4.png differ diff --git a/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step5.png b/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step5.png index 6a07b46e4..165bffb90 100644 Binary files a/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step5.png and b/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step5.png differ diff --git a/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step6.png b/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step6.png index 7fa2eaf70..161bee4bc 100644 Binary files a/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step6.png and b/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step6.png differ diff --git a/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step7.png b/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step7.png index 552e9f959..2bb9624c4 100644 Binary files a/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step7.png and b/docs/chapter_sorting/bubble_sort.assets/bubble_operation_step7.png differ