Skip to content

Commit

Permalink
[Rust] Normalize mid calculation in case overflow (#1363)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
rongyi and krahets authored May 18, 2024
1 parent 0e22154 commit 21be3fd
Show file tree
Hide file tree
Showing 41 changed files with 57 additions and 59 deletions.
2 changes: 1 addition & 1 deletion codes/c/chapter_sorting/merge_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -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); // 递归右子数组
// 合并阶段
Expand Down
2 changes: 1 addition & 1 deletion codes/cpp/chapter_sorting/merge_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void mergeSort(vector<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); // 递归右子数组
// 合并阶段
Expand Down
2 changes: 1 addition & 1 deletion codes/csharp/chapter_sorting/merge_sort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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); // 递归右子数组
// 合并阶段
Expand Down
2 changes: 1 addition & 1 deletion codes/dart/chapter_sorting/merge_sort.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void mergeSort(List<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); // 递归右子数组
// 合并阶段
Expand Down
2 changes: 1 addition & 1 deletion codes/go/chapter_sorting/merge_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
// 合并阶段
Expand Down
2 changes: 1 addition & 1 deletion codes/java/chapter_sorting/merge_sort.java
Original file line number Diff line number Diff line change
Expand Up @@ -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); // 递归右子数组
// 合并阶段
Expand Down
2 changes: 1 addition & 1 deletion codes/javascript/chapter_sorting/merge_sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); // 递归右子数组
// 合并阶段
Expand Down
4 changes: 2 additions & 2 deletions codes/kotlin/chapter_sorting/merge_sort.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) // 递归右子数组
// 合并阶段
Expand All @@ -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()}")
}
}
2 changes: 1 addition & 1 deletion codes/python/chapter_sorting/merge_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) # 递归右子数组
# 合并阶段
Expand Down
2 changes: 1 addition & 1 deletion codes/ruby/chapter_sorting/merge_sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) # 递归右子数组
# 合并阶段
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_sorting/merge_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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); // 递归右子数组

Expand Down
4 changes: 1 addition & 3 deletions codes/rust/chapter_sorting/radix_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/* 基数排序 */
Expand Down
2 changes: 1 addition & 1 deletion codes/swift/chapter_sorting/merge_sort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) // 递归右子数组
// 合并阶段
Expand Down
2 changes: 1 addition & 1 deletion codes/swift/chapter_sorting/quick_sort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion codes/typescript/chapter_sorting/merge_sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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); // 递归右子数组
// 合并阶段
Expand Down
8 changes: 4 additions & 4 deletions codes/zig/chapter_sorting/merge_sort.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 分别指向左子数组、右子数组的首元素
Expand Down Expand Up @@ -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); // 递归右子数组
// 合并阶段
Expand All @@ -64,4 +64,4 @@ pub fn main() !void {
inc.PrintUtil.printArray(i32, &nums);

_ = try std.io.getStdIn().reader().readByte();
}
}
6 changes: 3 additions & 3 deletions codes/zig/chapter_tree/binary_search_tree.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
// 递归建立左子树和右子树
Expand Down Expand Up @@ -145,7 +145,7 @@ pub fn BinarySearchTree(comptime T: type) type {
cur.?.val = tmp_val;
}
}
};
};
}

// Driver Code
Expand Down Expand Up @@ -179,4 +179,4 @@ pub fn main() !void {
try inc.PrintUtil.printTree(bst.getRoot(), null, false);

_ = try std.io.getStdIn().reader().readByte();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int dfs(vector<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);
Expand Down Expand Up @@ -43,4 +43,4 @@ int main() {
cout << "Index of target element 6 =" << index << endl;

return 0;
}
}
2 changes: 1 addition & 1 deletion en/codes/cpp/chapter_sorting/merge_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void mergeSort(vector<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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion en/codes/java/chapter_sorting/merge_sort.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions en/codes/python/chapter_searching/binary_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions en/codes/python/chapter_searching/binary_search_insertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion en/codes/python/chapter_sorting/merge_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/c/chapter_sorting/merge_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -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); // 遞迴右子陣列
// 合併階段
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/cpp/chapter_sorting/merge_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void mergeSort(vector<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); // 遞迴右子陣列
// 合併階段
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/csharp/chapter_sorting/merge_sort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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); // 遞迴右子陣列
// 合併階段
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/dart/chapter_sorting/merge_sort.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void mergeSort(List<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); // 遞迴右子陣列
// 合併階段
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/go/chapter_sorting/merge_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
// 合併階段
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/java/chapter_sorting/merge_sort.java
Original file line number Diff line number Diff line change
Expand Up @@ -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); // 遞迴右子陣列
// 合併階段
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/javascript/chapter_sorting/merge_sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); // 遞迴右子陣列
// 合併階段
Expand Down
6 changes: 3 additions & 3 deletions zh-hant/codes/kotlin/chapter_sorting/merge_sort.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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++]
}
// 將左子陣列和右子陣列的剩餘元素複製到臨時陣列中
Expand All @@ -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) // 遞迴右子陣列
// 合併階段
Expand All @@ -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()}")
}
}
2 changes: 1 addition & 1 deletion zh-hant/codes/python/chapter_sorting/merge_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) # 遞迴右子陣列
# 合併階段
Expand Down
Loading

0 comments on commit 21be3fd

Please sign in to comment.