From 21be3fdaf8b5d5f167f18fbd151a9ab36562c808 Mon Sep 17 00:00:00 2001 From: rongyi Date: Sat, 18 May 2024 18:19:19 +0800 Subject: [PATCH] [Rust] Normalize mid calculation in case overflow (#1363) * Normalize mid calculate in case overflow * Change ALL language * Update merge_sort.py * Update merge_sort.zig * Update binary_search_tree.zig * Update binary_search_recur.py --------- Co-authored-by: Yudong Jin --- codes/c/chapter_sorting/merge_sort.c | 2 +- codes/cpp/chapter_sorting/merge_sort.cpp | 2 +- codes/csharp/chapter_sorting/merge_sort.cs | 2 +- codes/dart/chapter_sorting/merge_sort.dart | 2 +- codes/go/chapter_sorting/merge_sort.go | 2 +- codes/java/chapter_sorting/merge_sort.java | 2 +- codes/javascript/chapter_sorting/merge_sort.js | 2 +- codes/kotlin/chapter_sorting/merge_sort.kt | 4 ++-- codes/python/chapter_sorting/merge_sort.py | 2 +- codes/ruby/chapter_sorting/merge_sort.rb | 2 +- .../chapter_divide_and_conquer/binary_search_recur.rs | 2 +- codes/rust/chapter_sorting/merge_sort.rs | 2 +- codes/rust/chapter_sorting/radix_sort.rs | 4 +--- codes/swift/chapter_sorting/merge_sort.swift | 2 +- codes/swift/chapter_sorting/quick_sort.swift | 2 +- codes/typescript/chapter_sorting/merge_sort.ts | 2 +- codes/zig/chapter_sorting/merge_sort.zig | 8 ++++---- codes/zig/chapter_tree/binary_search_tree.zig | 6 +++--- .../chapter_divide_and_conquer/binary_search_recur.cpp | 4 ++-- en/codes/cpp/chapter_sorting/merge_sort.cpp | 2 +- .../chapter_divide_and_conquer/binary_search_recur.java | 2 +- en/codes/java/chapter_sorting/merge_sort.java | 2 +- en/codes/python/chapter_searching/binary_search.py | 4 ++-- .../python/chapter_searching/binary_search_insertion.py | 4 ++-- en/codes/python/chapter_sorting/merge_sort.py | 2 +- zh-hant/codes/c/chapter_sorting/merge_sort.c | 2 +- zh-hant/codes/cpp/chapter_sorting/merge_sort.cpp | 2 +- zh-hant/codes/csharp/chapter_sorting/merge_sort.cs | 2 +- zh-hant/codes/dart/chapter_sorting/merge_sort.dart | 2 +- zh-hant/codes/go/chapter_sorting/merge_sort.go | 2 +- zh-hant/codes/java/chapter_sorting/merge_sort.java | 2 +- zh-hant/codes/javascript/chapter_sorting/merge_sort.js | 2 +- zh-hant/codes/kotlin/chapter_sorting/merge_sort.kt | 6 +++--- zh-hant/codes/python/chapter_sorting/merge_sort.py | 2 +- zh-hant/codes/ruby/chapter_sorting/merge_sort.rb | 2 +- zh-hant/codes/rust/chapter_sorting/merge_sort.rs | 2 +- zh-hant/codes/swift/chapter_sorting/merge_sort.swift | 2 +- zh-hant/codes/swift/chapter_sorting/quick_sort.swift | 2 +- zh-hant/codes/typescript/chapter_sorting/merge_sort.ts | 2 +- zh-hant/codes/zig/chapter_sorting/merge_sort.zig | 8 ++++---- zh-hant/codes/zig/chapter_tree/binary_search_tree.zig | 6 +++--- 41 files changed, 57 insertions(+), 59 deletions(-) diff --git a/codes/c/chapter_sorting/merge_sort.c b/codes/c/chapter_sorting/merge_sort.c index 7ff28c804f..e4bc254a55 100644 --- a/codes/c/chapter_sorting/merge_sort.c +++ b/codes/c/chapter_sorting/merge_sort.c @@ -43,7 +43,7 @@ void mergeSort(int *nums, int left, int right) { if (left >= right) return; // 当子数组长度为 1 时终止递归 // 划分阶段 - int mid = (left + right) / 2; // 计算中点 + int mid = left + (right - left) / 2; // 计算中点 mergeSort(nums, left, mid); // 递归左子数组 mergeSort(nums, mid + 1, right); // 递归右子数组 // 合并阶段 diff --git a/codes/cpp/chapter_sorting/merge_sort.cpp b/codes/cpp/chapter_sorting/merge_sort.cpp index 98a2795c46..827b9f1f47 100644 --- a/codes/cpp/chapter_sorting/merge_sort.cpp +++ b/codes/cpp/chapter_sorting/merge_sort.cpp @@ -39,7 +39,7 @@ void mergeSort(vector &nums, int left, int right) { if (left >= right) return; // 当子数组长度为 1 时终止递归 // 划分阶段 - int mid = (left + right) / 2; // 计算中点 + int mid = left + (right - left) / 2; // 计算中点 mergeSort(nums, left, mid); // 递归左子数组 mergeSort(nums, mid + 1, right); // 递归右子数组 // 合并阶段 diff --git a/codes/csharp/chapter_sorting/merge_sort.cs b/codes/csharp/chapter_sorting/merge_sort.cs index 86de13067b..051675d8d3 100644 --- a/codes/csharp/chapter_sorting/merge_sort.cs +++ b/codes/csharp/chapter_sorting/merge_sort.cs @@ -39,7 +39,7 @@ void MergeSort(int[] nums, int left, int right) { // 终止条件 if (left >= right) return; // 当子数组长度为 1 时终止递归 // 划分阶段 - int mid = (left + right) / 2; // 计算中点 + int mid = left + (right - left) / 2; // 计算中点 MergeSort(nums, left, mid); // 递归左子数组 MergeSort(nums, mid + 1, right); // 递归右子数组 // 合并阶段 diff --git a/codes/dart/chapter_sorting/merge_sort.dart b/codes/dart/chapter_sorting/merge_sort.dart index 03a0e1f682..68fc3b34b8 100644 --- a/codes/dart/chapter_sorting/merge_sort.dart +++ b/codes/dart/chapter_sorting/merge_sort.dart @@ -36,7 +36,7 @@ void mergeSort(List nums, int left, int right) { // 终止条件 if (left >= right) return; // 当子数组长度为 1 时终止递归 // 划分阶段 - int mid = (left + right) ~/ 2; // 计算中点 + int mid = left + (right - left) ~/ 2; // 计算中点 mergeSort(nums, left, mid); // 递归左子数组 mergeSort(nums, mid + 1, right); // 递归右子数组 // 合并阶段 diff --git a/codes/go/chapter_sorting/merge_sort.go b/codes/go/chapter_sorting/merge_sort.go index 54f2bbe325..252618fbda 100644 --- a/codes/go/chapter_sorting/merge_sort.go +++ b/codes/go/chapter_sorting/merge_sort.go @@ -46,7 +46,7 @@ func mergeSort(nums []int, left, right int) { return } // 划分阶段 - mid := (left + right) / 2 + mid := left + (right - left) / 2 mergeSort(nums, left, mid) mergeSort(nums, mid+1, right) // 合并阶段 diff --git a/codes/java/chapter_sorting/merge_sort.java b/codes/java/chapter_sorting/merge_sort.java index 8fe24f79dd..e35a0445b6 100644 --- a/codes/java/chapter_sorting/merge_sort.java +++ b/codes/java/chapter_sorting/merge_sort.java @@ -42,7 +42,7 @@ static void mergeSort(int[] nums, int left, int right) { if (left >= right) return; // 当子数组长度为 1 时终止递归 // 划分阶段 - int mid = (left + right) / 2; // 计算中点 + int mid = left + (right - left) / 2; // 计算中点 mergeSort(nums, left, mid); // 递归左子数组 mergeSort(nums, mid + 1, right); // 递归右子数组 // 合并阶段 diff --git a/codes/javascript/chapter_sorting/merge_sort.js b/codes/javascript/chapter_sorting/merge_sort.js index 033df1b8be..107c09fb88 100644 --- a/codes/javascript/chapter_sorting/merge_sort.js +++ b/codes/javascript/chapter_sorting/merge_sort.js @@ -39,7 +39,7 @@ function mergeSort(nums, left, right) { // 终止条件 if (left >= right) return; // 当子数组长度为 1 时终止递归 // 划分阶段 - let mid = Math.floor((left + right) / 2); // 计算中点 + let mid = Math.floor(left + (right - left) / 2); // 计算中点 mergeSort(nums, left, mid); // 递归左子数组 mergeSort(nums, mid + 1, right); // 递归右子数组 // 合并阶段 diff --git a/codes/kotlin/chapter_sorting/merge_sort.kt b/codes/kotlin/chapter_sorting/merge_sort.kt index 16a15c9420..40ca9ca979 100644 --- a/codes/kotlin/chapter_sorting/merge_sort.kt +++ b/codes/kotlin/chapter_sorting/merge_sort.kt @@ -40,7 +40,7 @@ fun mergeSort(nums: IntArray, left: Int, right: Int) { // 终止条件 if (left >= right) return // 当子数组长度为 1 时终止递归 // 划分阶段 - val mid = (left + right) / 2 // 计算中点 + val mid = left + (right - left) / 2 // 计算中点 mergeSort(nums, left, mid) // 递归左子数组 mergeSort(nums, mid + 1, right) // 递归右子数组 // 合并阶段 @@ -53,4 +53,4 @@ fun main() { val nums = intArrayOf(7, 3, 2, 6, 0, 1, 5, 4) mergeSort(nums, 0, nums.size - 1) println("归并排序完成后 nums = ${nums.contentToString()}") -} \ No newline at end of file +} diff --git a/codes/python/chapter_sorting/merge_sort.py b/codes/python/chapter_sorting/merge_sort.py index e6e009e5fe..dbd32e6656 100644 --- a/codes/python/chapter_sorting/merge_sort.py +++ b/codes/python/chapter_sorting/merge_sort.py @@ -41,7 +41,7 @@ def merge_sort(nums: list[int], left: int, right: int): if left >= right: return # 当子数组长度为 1 时终止递归 # 划分阶段 - mid = (left + right) // 2 # 计算中点 + mid = (left + right) // 2 # 计算中点 merge_sort(nums, left, mid) # 递归左子数组 merge_sort(nums, mid + 1, right) # 递归右子数组 # 合并阶段 diff --git a/codes/ruby/chapter_sorting/merge_sort.rb b/codes/ruby/chapter_sorting/merge_sort.rb index 2e90dd8951..0bb2d1f526 100644 --- a/codes/ruby/chapter_sorting/merge_sort.rb +++ b/codes/ruby/chapter_sorting/merge_sort.rb @@ -45,7 +45,7 @@ def merge_sort(nums, left, right) # 当子数组长度为 1 时终止递归 return if left >= right # 划分阶段 - mid = (left + right) / 2 # 计算中点 + mid = left + (right - left) / 2 # 计算中点 merge_sort(nums, left, mid) # 递归左子数组 merge_sort(nums, mid + 1, right) # 递归右子数组 # 合并阶段 diff --git a/codes/rust/chapter_divide_and_conquer/binary_search_recur.rs b/codes/rust/chapter_divide_and_conquer/binary_search_recur.rs index 4130a147d6..b0864cbca6 100644 --- a/codes/rust/chapter_divide_and_conquer/binary_search_recur.rs +++ b/codes/rust/chapter_divide_and_conquer/binary_search_recur.rs @@ -10,7 +10,7 @@ fn dfs(nums: &[i32], target: i32, i: i32, j: i32) -> i32 { if i > j { return -1; } - let m: i32 = (i + j) / 2; + let m: i32 = i + (j - i) / 2; if nums[m as usize] < target { // 递归子问题 f(m+1, j) return dfs(nums, target, m + 1, j); diff --git a/codes/rust/chapter_sorting/merge_sort.rs b/codes/rust/chapter_sorting/merge_sort.rs index 02894bd332..6b28e8b011 100644 --- a/codes/rust/chapter_sorting/merge_sort.rs +++ b/codes/rust/chapter_sorting/merge_sort.rs @@ -48,7 +48,7 @@ fn merge_sort(nums: &mut [i32], left: usize, right: usize) { } // 划分阶段 - let mid = (left + right) / 2; // 计算中点 + let mid = left + (right - left) / 2; // 计算中点 merge_sort(nums, left, mid); // 递归左子数组 merge_sort(nums, mid + 1, right); // 递归右子数组 diff --git a/codes/rust/chapter_sorting/radix_sort.rs b/codes/rust/chapter_sorting/radix_sort.rs index d9c151be35..722826e1e4 100644 --- a/codes/rust/chapter_sorting/radix_sort.rs +++ b/codes/rust/chapter_sorting/radix_sort.rs @@ -35,9 +35,7 @@ fn counting_sort_digit(nums: &mut [i32], exp: i32) { counter[d] -= 1; // 将 d 的数量减 1 } // 使用结果覆盖原数组 nums - for i in 0..n { - nums[i] = res[i]; - } + nums.copy_from_slice(&res); } /* 基数排序 */ diff --git a/codes/swift/chapter_sorting/merge_sort.swift b/codes/swift/chapter_sorting/merge_sort.swift index e587c15785..abc84fb1ec 100644 --- a/codes/swift/chapter_sorting/merge_sort.swift +++ b/codes/swift/chapter_sorting/merge_sort.swift @@ -46,7 +46,7 @@ func mergeSort(nums: inout [Int], left: Int, right: Int) { return } // 划分阶段 - let mid = (left + right) / 2 // 计算中点 + let mid = left + (right - left) / 2 // 计算中点 mergeSort(nums: &nums, left: left, right: mid) // 递归左子数组 mergeSort(nums: &nums, left: mid + 1, right: right) // 递归右子数组 // 合并阶段 diff --git a/codes/swift/chapter_sorting/quick_sort.swift b/codes/swift/chapter_sorting/quick_sort.swift index 957e86471c..d4bcf9393e 100644 --- a/codes/swift/chapter_sorting/quick_sort.swift +++ b/codes/swift/chapter_sorting/quick_sort.swift @@ -54,7 +54,7 @@ func medianThree(nums: [Int], left: Int, mid: Int, right: Int) -> Int { /* 哨兵划分(三数取中值) */ func partitionMedian(nums: inout [Int], left: Int, right: Int) -> Int { // 选取三个候选元素的中位数 - let med = medianThree(nums: nums, left: left, mid: (left + right) / 2, right: right) + let med = medianThree(nums: nums, left: left, mid: left + (right - left) / 2, right: right) // 将中位数交换至数组最左端 nums.swapAt(left, med) return partition(nums: &nums, left: left, right: right) diff --git a/codes/typescript/chapter_sorting/merge_sort.ts b/codes/typescript/chapter_sorting/merge_sort.ts index 9fc0c4adb1..affef4d78f 100644 --- a/codes/typescript/chapter_sorting/merge_sort.ts +++ b/codes/typescript/chapter_sorting/merge_sort.ts @@ -39,7 +39,7 @@ function mergeSort(nums: number[], left: number, right: number): void { // 终止条件 if (left >= right) return; // 当子数组长度为 1 时终止递归 // 划分阶段 - let mid = Math.floor((left + right) / 2); // 计算中点 + let mid = Math.floor(left + (right - left) / 2); // 计算中点 mergeSort(nums, left, mid); // 递归左子数组 mergeSort(nums, mid + 1, right); // 递归右子数组 // 合并阶段 diff --git a/codes/zig/chapter_sorting/merge_sort.zig b/codes/zig/chapter_sorting/merge_sort.zig index aa47172d55..ac0d6245b3 100644 --- a/codes/zig/chapter_sorting/merge_sort.zig +++ b/codes/zig/chapter_sorting/merge_sort.zig @@ -15,10 +15,10 @@ fn merge(nums: []i32, left: usize, mid: usize, right: usize) !void { const mem_allocator = mem_arena.allocator(); var tmp = try mem_allocator.alloc(i32, right + 1 - left); std.mem.copy(i32, tmp, nums[left..right+1]); - // 左子数组的起始索引和结束索引 + // 左子数组的起始索引和结束索引 var leftStart = left - left; var leftEnd = mid - left; - // 右子数组的起始索引和结束索引 + // 右子数组的起始索引和结束索引 var rightStart = mid + 1 - left; var rightEnd = right - left; // i, j 分别指向左子数组、右子数组的首元素 @@ -48,7 +48,7 @@ fn mergeSort(nums: []i32, left: usize, right: usize) !void { // 终止条件 if (left >= right) return; // 当子数组长度为 1 时终止递归 // 划分阶段 - var mid = (left + right) / 2; // 计算中点 + var mid = left + (right - left) / 2; // 计算中点 try mergeSort(nums, left, mid); // 递归左子数组 try mergeSort(nums, mid + 1, right); // 递归右子数组 // 合并阶段 @@ -64,4 +64,4 @@ pub fn main() !void { inc.PrintUtil.printArray(i32, &nums); _ = try std.io.getStdIn().reader().readByte(); -} \ No newline at end of file +} diff --git a/codes/zig/chapter_tree/binary_search_tree.zig b/codes/zig/chapter_tree/binary_search_tree.zig index b3b2a728e7..bfc4a6e590 100644 --- a/codes/zig/chapter_tree/binary_search_tree.zig +++ b/codes/zig/chapter_tree/binary_search_tree.zig @@ -34,7 +34,7 @@ pub fn BinarySearchTree(comptime T: type) type { fn buildTree(self: *Self, nums: []T, i: usize, j: usize) !?*inc.TreeNode(T) { if (i > j) return null; // 将数组中间节点作为根节点 - var mid = (i + j) / 2; + var mid = i + (j - i) / 2; var node = try self.mem_allocator.create(inc.TreeNode(T)); node.init(nums[mid]); // 递归建立左子树和右子树 @@ -145,7 +145,7 @@ pub fn BinarySearchTree(comptime T: type) type { cur.?.val = tmp_val; } } - }; + }; } // Driver Code @@ -179,4 +179,4 @@ pub fn main() !void { try inc.PrintUtil.printTree(bst.getRoot(), null, false); _ = try std.io.getStdIn().reader().readByte(); -} \ No newline at end of file +} diff --git a/en/codes/cpp/chapter_divide_and_conquer/binary_search_recur.cpp b/en/codes/cpp/chapter_divide_and_conquer/binary_search_recur.cpp index 4a5b67f477..78f9e8333e 100644 --- a/en/codes/cpp/chapter_divide_and_conquer/binary_search_recur.cpp +++ b/en/codes/cpp/chapter_divide_and_conquer/binary_search_recur.cpp @@ -13,7 +13,7 @@ int dfs(vector &nums, int target, int i, int j) { return -1; } // Calculate midpoint index m - int m = (i + j) / 2; + int m = i + (j - i) / 2; if (nums[m] < target) { // Recursive subproblem f(m+1, j) return dfs(nums, target, m + 1, j); @@ -43,4 +43,4 @@ int main() { cout << "Index of target element 6 =" << index << endl; return 0; -} \ No newline at end of file +} diff --git a/en/codes/cpp/chapter_sorting/merge_sort.cpp b/en/codes/cpp/chapter_sorting/merge_sort.cpp index dbd45dc8a1..0d320c0050 100644 --- a/en/codes/cpp/chapter_sorting/merge_sort.cpp +++ b/en/codes/cpp/chapter_sorting/merge_sort.cpp @@ -39,7 +39,7 @@ void mergeSort(vector &nums, int left, int right) { if (left >= right) return; // Terminate recursion when subarray length is 1 // Partition stage - int mid = (left + right) / 2; // Calculate midpoint + int mid = left + (right - left) / 2; // Calculate midpoint mergeSort(nums, left, mid); // Recursively process the left subarray mergeSort(nums, mid + 1, right); // Recursively process the right subarray // Merge stage diff --git a/en/codes/java/chapter_divide_and_conquer/binary_search_recur.java b/en/codes/java/chapter_divide_and_conquer/binary_search_recur.java index 0fbb3b80fe..d80340100b 100644 --- a/en/codes/java/chapter_divide_and_conquer/binary_search_recur.java +++ b/en/codes/java/chapter_divide_and_conquer/binary_search_recur.java @@ -14,7 +14,7 @@ static int dfs(int[] nums, int target, int i, int j) { return -1; } // Calculate midpoint index m - int m = (i + j) / 2; + int m = i + (j - i) / 2; if (nums[m] < target) { // Recursive subproblem f(m+1, j) return dfs(nums, target, m + 1, j); diff --git a/en/codes/java/chapter_sorting/merge_sort.java b/en/codes/java/chapter_sorting/merge_sort.java index d4bebc0bab..3c6f3f2bff 100644 --- a/en/codes/java/chapter_sorting/merge_sort.java +++ b/en/codes/java/chapter_sorting/merge_sort.java @@ -42,7 +42,7 @@ static void mergeSort(int[] nums, int left, int right) { if (left >= right) return; // Terminate recursion when subarray length is 1 // Partition stage - int mid = (left + right) / 2; // Calculate midpoint + int mid = left + (right - left) / 2; // Calculate midpoint mergeSort(nums, left, mid); // Recursively process the left subarray mergeSort(nums, mid + 1, right); // Recursively process the right subarray // Merge stage diff --git a/en/codes/python/chapter_searching/binary_search.py b/en/codes/python/chapter_searching/binary_search.py index fd0d9c078d..ec40fa69c3 100644 --- a/en/codes/python/chapter_searching/binary_search.py +++ b/en/codes/python/chapter_searching/binary_search.py @@ -12,7 +12,7 @@ def binary_search(nums: list[int], target: int) -> int: # Loop until the search interval is empty (when i > j, it is empty) while i <= j: # Theoretically, Python's numbers can be infinitely large (depending on memory size), so there is no need to consider large number overflow - m = (i + j) // 2 # Calculate midpoint index m + m = i + (j - i) // 2 # Calculate midpoint index m if nums[m] < target: i = m + 1 # This situation indicates that target is in the interval [m+1, j] elif nums[m] > target: @@ -28,7 +28,7 @@ def binary_search_lcro(nums: list[int], target: int) -> int: i, j = 0, len(nums) # Loop until the search interval is empty (when i = j, it is empty) while i < j: - m = (i + j) // 2 # Calculate midpoint index m + m = i + (j - i) // 2 # Calculate midpoint index m if nums[m] < target: i = m + 1 # This situation indicates that target is in the interval [m+1, j) elif nums[m] > target: diff --git a/en/codes/python/chapter_searching/binary_search_insertion.py b/en/codes/python/chapter_searching/binary_search_insertion.py index e63f9e6c6c..c532bc0ebd 100644 --- a/en/codes/python/chapter_searching/binary_search_insertion.py +++ b/en/codes/python/chapter_searching/binary_search_insertion.py @@ -9,7 +9,7 @@ def binary_search_insertion_simple(nums: list[int], target: int) -> int: """Binary search for insertion point (no duplicate elements)""" i, j = 0, len(nums) - 1 # Initialize double closed interval [0, n-1] while i <= j: - m = (i + j) // 2 # Calculate midpoint index m + m = i + (j - i) // 2 # Calculate midpoint index m if nums[m] < target: i = m + 1 # Target is in interval [m+1, j] elif nums[m] > target: @@ -24,7 +24,7 @@ def binary_search_insertion(nums: list[int], target: int) -> int: """Binary search for insertion point (with duplicate elements)""" i, j = 0, len(nums) - 1 # Initialize double closed interval [0, n-1] while i <= j: - m = (i + j) // 2 # Calculate midpoint index m + m = i + (j - i) // 2 # Calculate midpoint index m if nums[m] < target: i = m + 1 # Target is in interval [m+1, j] elif nums[m] > target: diff --git a/en/codes/python/chapter_sorting/merge_sort.py b/en/codes/python/chapter_sorting/merge_sort.py index 18a1d9ddc9..863618844c 100644 --- a/en/codes/python/chapter_sorting/merge_sort.py +++ b/en/codes/python/chapter_sorting/merge_sort.py @@ -41,7 +41,7 @@ def merge_sort(nums: list[int], left: int, right: int): if left >= right: return # Terminate recursion when subarray length is 1 # Partition stage - mid = (left + right) // 2 # Calculate midpoint + mid = left + (right - left) // 2 # Calculate midpoint merge_sort(nums, left, mid) # Recursively process the left subarray merge_sort(nums, mid + 1, right) # Recursively process the right subarray # Merge stage diff --git a/zh-hant/codes/c/chapter_sorting/merge_sort.c b/zh-hant/codes/c/chapter_sorting/merge_sort.c index 1ca94cdb29..996148ea90 100644 --- a/zh-hant/codes/c/chapter_sorting/merge_sort.c +++ b/zh-hant/codes/c/chapter_sorting/merge_sort.c @@ -43,7 +43,7 @@ void mergeSort(int *nums, int left, int right) { if (left >= right) return; // 當子陣列長度為 1 時終止遞迴 // 劃分階段 - int mid = (left + right) / 2; // 計算中點 + int mid = left + (right - left) / 2; // 計算中點 mergeSort(nums, left, mid); // 遞迴左子陣列 mergeSort(nums, mid + 1, right); // 遞迴右子陣列 // 合併階段 diff --git a/zh-hant/codes/cpp/chapter_sorting/merge_sort.cpp b/zh-hant/codes/cpp/chapter_sorting/merge_sort.cpp index c0586a3a9e..5d32b42124 100644 --- a/zh-hant/codes/cpp/chapter_sorting/merge_sort.cpp +++ b/zh-hant/codes/cpp/chapter_sorting/merge_sort.cpp @@ -39,7 +39,7 @@ void mergeSort(vector &nums, int left, int right) { if (left >= right) return; // 當子陣列長度為 1 時終止遞迴 // 劃分階段 - int mid = (left + right) / 2; // 計算中點 + int mid = left + (right - left) / 2; // 計算中點 mergeSort(nums, left, mid); // 遞迴左子陣列 mergeSort(nums, mid + 1, right); // 遞迴右子陣列 // 合併階段 diff --git a/zh-hant/codes/csharp/chapter_sorting/merge_sort.cs b/zh-hant/codes/csharp/chapter_sorting/merge_sort.cs index a22c1f891d..22e1bf0144 100644 --- a/zh-hant/codes/csharp/chapter_sorting/merge_sort.cs +++ b/zh-hant/codes/csharp/chapter_sorting/merge_sort.cs @@ -39,7 +39,7 @@ void MergeSort(int[] nums, int left, int right) { // 終止條件 if (left >= right) return; // 當子陣列長度為 1 時終止遞迴 // 劃分階段 - int mid = (left + right) / 2; // 計算中點 + int mid = left + (right - left) / 2; // 計算中點 MergeSort(nums, left, mid); // 遞迴左子陣列 MergeSort(nums, mid + 1, right); // 遞迴右子陣列 // 合併階段 diff --git a/zh-hant/codes/dart/chapter_sorting/merge_sort.dart b/zh-hant/codes/dart/chapter_sorting/merge_sort.dart index a492de39be..6b953ca342 100644 --- a/zh-hant/codes/dart/chapter_sorting/merge_sort.dart +++ b/zh-hant/codes/dart/chapter_sorting/merge_sort.dart @@ -36,7 +36,7 @@ void mergeSort(List nums, int left, int right) { // 終止條件 if (left >= right) return; // 當子陣列長度為 1 時終止遞迴 // 劃分階段 - int mid = (left + right) ~/ 2; // 計算中點 + int mid = left + (right - left) ~/ 2; // 計算中點 mergeSort(nums, left, mid); // 遞迴左子陣列 mergeSort(nums, mid + 1, right); // 遞迴右子陣列 // 合併階段 diff --git a/zh-hant/codes/go/chapter_sorting/merge_sort.go b/zh-hant/codes/go/chapter_sorting/merge_sort.go index e5daa81b9e..49e880ef3d 100644 --- a/zh-hant/codes/go/chapter_sorting/merge_sort.go +++ b/zh-hant/codes/go/chapter_sorting/merge_sort.go @@ -46,7 +46,7 @@ func mergeSort(nums []int, left, right int) { return } // 劃分階段 - mid := (left + right) / 2 + mid := left + (right - left) / 2 mergeSort(nums, left, mid) mergeSort(nums, mid+1, right) // 合併階段 diff --git a/zh-hant/codes/java/chapter_sorting/merge_sort.java b/zh-hant/codes/java/chapter_sorting/merge_sort.java index 8ba74edf04..df89a4830c 100644 --- a/zh-hant/codes/java/chapter_sorting/merge_sort.java +++ b/zh-hant/codes/java/chapter_sorting/merge_sort.java @@ -42,7 +42,7 @@ static void mergeSort(int[] nums, int left, int right) { if (left >= right) return; // 當子陣列長度為 1 時終止遞迴 // 劃分階段 - int mid = (left + right) / 2; // 計算中點 + int mid = left + (right - left) / 2; // 計算中點 mergeSort(nums, left, mid); // 遞迴左子陣列 mergeSort(nums, mid + 1, right); // 遞迴右子陣列 // 合併階段 diff --git a/zh-hant/codes/javascript/chapter_sorting/merge_sort.js b/zh-hant/codes/javascript/chapter_sorting/merge_sort.js index 0a14ea99bf..1b5f1b2bc9 100644 --- a/zh-hant/codes/javascript/chapter_sorting/merge_sort.js +++ b/zh-hant/codes/javascript/chapter_sorting/merge_sort.js @@ -39,7 +39,7 @@ function mergeSort(nums, left, right) { // 終止條件 if (left >= right) return; // 當子陣列長度為 1 時終止遞迴 // 劃分階段 - let mid = Math.floor((left + right) / 2); // 計算中點 + let mid = Math.floor(left + (right - left) / 2); // 計算中點 mergeSort(nums, left, mid); // 遞迴左子陣列 mergeSort(nums, mid + 1, right); // 遞迴右子陣列 // 合併階段 diff --git a/zh-hant/codes/kotlin/chapter_sorting/merge_sort.kt b/zh-hant/codes/kotlin/chapter_sorting/merge_sort.kt index 0c8f9da4b9..8b328e50eb 100644 --- a/zh-hant/codes/kotlin/chapter_sorting/merge_sort.kt +++ b/zh-hant/codes/kotlin/chapter_sorting/merge_sort.kt @@ -19,7 +19,7 @@ fun merge(nums: IntArray, left: Int, mid: Int, right: Int) { while (i <= mid && j <= right) { if (nums[i] <= nums[j]) tmp[k++] = nums[i++] - else + else tmp[k++] = nums[j++] } // 將左子陣列和右子陣列的剩餘元素複製到臨時陣列中 @@ -40,7 +40,7 @@ fun mergeSort(nums: IntArray, left: Int, right: Int) { // 終止條件 if (left >= right) return // 當子陣列長度為 1 時終止遞迴 // 劃分階段 - val mid = (left + right) / 2 // 計算中點 + val mid = left + (right - left) / 2 // 計算中點 mergeSort(nums, left, mid) // 遞迴左子陣列 mergeSort(nums, mid + 1, right) // 遞迴右子陣列 // 合併階段 @@ -53,4 +53,4 @@ fun main() { val nums = intArrayOf(7, 3, 2, 6, 0, 1, 5, 4) mergeSort(nums, 0, nums.size - 1) println("合併排序完成後 nums = ${nums.contentToString()}") -} \ No newline at end of file +} diff --git a/zh-hant/codes/python/chapter_sorting/merge_sort.py b/zh-hant/codes/python/chapter_sorting/merge_sort.py index 6eb4db04cc..48d4f706dc 100644 --- a/zh-hant/codes/python/chapter_sorting/merge_sort.py +++ b/zh-hant/codes/python/chapter_sorting/merge_sort.py @@ -41,7 +41,7 @@ def merge_sort(nums: list[int], left: int, right: int): if left >= right: return # 當子陣列長度為 1 時終止遞迴 # 劃分階段 - mid = (left + right) // 2 # 計算中點 + mid = left + (right - left) // 2 # 計算中點 merge_sort(nums, left, mid) # 遞迴左子陣列 merge_sort(nums, mid + 1, right) # 遞迴右子陣列 # 合併階段 diff --git a/zh-hant/codes/ruby/chapter_sorting/merge_sort.rb b/zh-hant/codes/ruby/chapter_sorting/merge_sort.rb index 317fbe8abe..5316bb0987 100644 --- a/zh-hant/codes/ruby/chapter_sorting/merge_sort.rb +++ b/zh-hant/codes/ruby/chapter_sorting/merge_sort.rb @@ -45,7 +45,7 @@ def merge_sort(nums, left, right) # 當子陣列長度為 1 時終止遞迴 return if left >= right # 劃分階段 - mid = (left + right) / 2 # 計算中點 + mid = left + (right - left) / 2 # 計算中點 merge_sort(nums, left, mid) # 遞迴左子陣列 merge_sort(nums, mid + 1, right) # 遞迴右子陣列 # 合併階段 diff --git a/zh-hant/codes/rust/chapter_sorting/merge_sort.rs b/zh-hant/codes/rust/chapter_sorting/merge_sort.rs index 049a0fcf4a..5e75a127be 100644 --- a/zh-hant/codes/rust/chapter_sorting/merge_sort.rs +++ b/zh-hant/codes/rust/chapter_sorting/merge_sort.rs @@ -48,7 +48,7 @@ fn merge_sort(nums: &mut [i32], left: usize, right: usize) { } // 劃分階段 - let mid = (left + right) / 2; // 計算中點 + let mid = left + (right - left) / 2; // 計算中點 merge_sort(nums, left, mid); // 遞迴左子陣列 merge_sort(nums, mid + 1, right); // 遞迴右子陣列 diff --git a/zh-hant/codes/swift/chapter_sorting/merge_sort.swift b/zh-hant/codes/swift/chapter_sorting/merge_sort.swift index 190b76fa98..9784185898 100644 --- a/zh-hant/codes/swift/chapter_sorting/merge_sort.swift +++ b/zh-hant/codes/swift/chapter_sorting/merge_sort.swift @@ -46,7 +46,7 @@ func mergeSort(nums: inout [Int], left: Int, right: Int) { return } // 劃分階段 - let mid = (left + right) / 2 // 計算中點 + let mid = left + (right - left) / 2 // 計算中點 mergeSort(nums: &nums, left: left, right: mid) // 遞迴左子陣列 mergeSort(nums: &nums, left: mid + 1, right: right) // 遞迴右子陣列 // 合併階段 diff --git a/zh-hant/codes/swift/chapter_sorting/quick_sort.swift b/zh-hant/codes/swift/chapter_sorting/quick_sort.swift index 088a1d05ba..550b18bcd4 100644 --- a/zh-hant/codes/swift/chapter_sorting/quick_sort.swift +++ b/zh-hant/codes/swift/chapter_sorting/quick_sort.swift @@ -54,7 +54,7 @@ func medianThree(nums: [Int], left: Int, mid: Int, right: Int) -> Int { /* 哨兵劃分(三數取中值) */ func partitionMedian(nums: inout [Int], left: Int, right: Int) -> Int { // 選取三個候選元素的中位數 - let med = medianThree(nums: nums, left: left, mid: (left + right) / 2, right: right) + let med = medianThree(nums: nums, left: left, mid: left + (right - left) / 2, right: right) // 將中位數交換至陣列最左端 nums.swapAt(left, med) return partition(nums: &nums, left: left, right: right) diff --git a/zh-hant/codes/typescript/chapter_sorting/merge_sort.ts b/zh-hant/codes/typescript/chapter_sorting/merge_sort.ts index cc28d2b448..2696ab439f 100644 --- a/zh-hant/codes/typescript/chapter_sorting/merge_sort.ts +++ b/zh-hant/codes/typescript/chapter_sorting/merge_sort.ts @@ -39,7 +39,7 @@ function mergeSort(nums: number[], left: number, right: number): void { // 終止條件 if (left >= right) return; // 當子陣列長度為 1 時終止遞迴 // 劃分階段 - let mid = Math.floor((left + right) / 2); // 計算中點 + let mid = Math.floor(left + (right - left) / 2); // 計算中點 mergeSort(nums, left, mid); // 遞迴左子陣列 mergeSort(nums, mid + 1, right); // 遞迴右子陣列 // 合併階段 diff --git a/zh-hant/codes/zig/chapter_sorting/merge_sort.zig b/zh-hant/codes/zig/chapter_sorting/merge_sort.zig index 0d756c48a1..e0a12d11a2 100644 --- a/zh-hant/codes/zig/chapter_sorting/merge_sort.zig +++ b/zh-hant/codes/zig/chapter_sorting/merge_sort.zig @@ -15,10 +15,10 @@ fn merge(nums: []i32, left: usize, mid: usize, right: usize) !void { const mem_allocator = mem_arena.allocator(); var tmp = try mem_allocator.alloc(i32, right + 1 - left); std.mem.copy(i32, tmp, nums[left..right+1]); - // 左子陣列的起始索引和結束索引 + // 左子陣列的起始索引和結束索引 var leftStart = left - left; var leftEnd = mid - left; - // 右子陣列的起始索引和結束索引 + // 右子陣列的起始索引和結束索引 var rightStart = mid + 1 - left; var rightEnd = right - left; // i, j 分別指向左子陣列、右子陣列的首元素 @@ -48,7 +48,7 @@ fn mergeSort(nums: []i32, left: usize, right: usize) !void { // 終止條件 if (left >= right) return; // 當子陣列長度為 1 時終止遞迴 // 劃分階段 - var mid = (left + right) / 2; // 計算中點 + var mid = left + (right - left) / 2; // 計算中點 try mergeSort(nums, left, mid); // 遞迴左子陣列 try mergeSort(nums, mid + 1, right); // 遞迴右子陣列 // 合併階段 @@ -64,4 +64,4 @@ pub fn main() !void { inc.PrintUtil.printArray(i32, &nums); _ = try std.io.getStdIn().reader().readByte(); -} \ No newline at end of file +} diff --git a/zh-hant/codes/zig/chapter_tree/binary_search_tree.zig b/zh-hant/codes/zig/chapter_tree/binary_search_tree.zig index d996dcb1a8..fb74c8df6c 100644 --- a/zh-hant/codes/zig/chapter_tree/binary_search_tree.zig +++ b/zh-hant/codes/zig/chapter_tree/binary_search_tree.zig @@ -34,7 +34,7 @@ pub fn BinarySearchTree(comptime T: type) type { fn buildTree(self: *Self, nums: []T, i: usize, j: usize) !?*inc.TreeNode(T) { if (i > j) return null; // 將陣列中間節點作為根節點 - var mid = (i + j) / 2; + var mid = i + (j - i) / 2; var node = try self.mem_allocator.create(inc.TreeNode(T)); node.init(nums[mid]); // 遞迴建立左子樹和右子樹 @@ -145,7 +145,7 @@ pub fn BinarySearchTree(comptime T: type) type { cur.?.val = tmp_val; } } - }; + }; } // Driver Code @@ -179,4 +179,4 @@ pub fn main() !void { try inc.PrintUtil.printTree(bst.getRoot(), null, false); _ = try std.io.getStdIn().reader().readByte(); -} \ No newline at end of file +}