diff --git a/codes/dart/chapter_heap/my_heap.dart b/codes/dart/chapter_heap/my_heap.dart index 854e6fca..f45da899 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 f136dbb8..d9859bb5 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 d48471a5..fefb8e06 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 08ce0a6a..f66abb78 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 51d0bfa0..72d95541 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 ce5f694e..8aa7d1cc 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 079c53e1..83f41cd7 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 d8593777..97498a8a 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 a71bc29c..7465423d 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 0ae3f031..e84b6d5c 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 f62f74fc..c2f23e95 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 459a4522..9180d44f 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 ecad3567..0c560a3d 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 33a00cf5..5f03048e 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 228e8019..0229fe9f 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 06098434..daea0f26 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 0672abe3..714a3c7e 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 cb046641..c94ab8ae 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 49d6765b..6cd132dd 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 addd2748..94931fa9 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 69eac84d..54c75779 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 1ed9dd42..a813bca6 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 1a9a7ae3..3fe1e3ae 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 00000000..3ce0f4bf 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 00000000..1c120bfe 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 00000000..8b8c6c0d 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 435ce7f2..719ebd9e 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 a267d24d..824b8e9a 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 1066f1af..23f18d88 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 40a6cff7..71efbc98 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 ad6f69f1..d02d33ad 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 d8b521d1..fd5400fd 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 31865cbe..1a61cf99 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 6a07b46e..165bffb9 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 7fa2eaf7..161bee4b 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 552e9f95..2bb9624c 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