mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 04:06:28 +08:00
[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 <krahets@163.com>
This commit is contained in:
parent
0e221540a3
commit
21be3fdaf8
41 changed files with 57 additions and 59 deletions
|
@ -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); // 递归右子数组
|
||||
// 合并阶段
|
||||
|
|
|
@ -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); // 递归右子数组
|
||||
// 合并阶段
|
||||
|
|
|
@ -39,7 +39,7 @@ public class merge_sort {
|
|||
// 终止条件
|
||||
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); // 递归右子数组
|
||||
// 合并阶段
|
||||
|
|
|
@ -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); // 递归右子数组
|
||||
// 合并阶段
|
||||
|
|
|
@ -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)
|
||||
// 合并阶段
|
||||
|
|
|
@ -42,7 +42,7 @@ public class merge_sort {
|
|||
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); // 递归右子数组
|
||||
// 合并阶段
|
||||
|
|
|
@ -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); // 递归右子数组
|
||||
// 合并阶段
|
||||
|
|
|
@ -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) // 递归右子数组
|
||||
// 合并阶段
|
||||
|
|
|
@ -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) # 递归右子数组
|
||||
# 合并阶段
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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); // 递归右子数组
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/* 基数排序 */
|
||||
|
|
|
@ -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) // 递归右子数组
|
||||
// 合并阶段
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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); // 递归右子数组
|
||||
// 合并阶段
|
||||
|
|
|
@ -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); // 递归右子数组
|
||||
// 合并阶段
|
||||
|
|
|
@ -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]);
|
||||
// 递归建立左子树和右子树
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -14,7 +14,7 @@ public class binary_search_recur {
|
|||
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);
|
||||
|
|
|
@ -42,7 +42,7 @@ public class merge_sort {
|
|||
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
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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); // 遞迴右子陣列
|
||||
// 合併階段
|
||||
|
|
|
@ -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); // 遞迴右子陣列
|
||||
// 合併階段
|
||||
|
|
|
@ -39,7 +39,7 @@ public class merge_sort {
|
|||
// 終止條件
|
||||
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); // 遞迴右子陣列
|
||||
// 合併階段
|
||||
|
|
|
@ -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); // 遞迴右子陣列
|
||||
// 合併階段
|
||||
|
|
|
@ -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)
|
||||
// 合併階段
|
||||
|
|
|
@ -42,7 +42,7 @@ public class merge_sort {
|
|||
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); // 遞迴右子陣列
|
||||
// 合併階段
|
||||
|
|
|
@ -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); // 遞迴右子陣列
|
||||
// 合併階段
|
||||
|
|
|
@ -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) // 遞迴右子陣列
|
||||
// 合併階段
|
||||
|
|
|
@ -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) # 遞迴右子陣列
|
||||
# 合併階段
|
||||
|
|
|
@ -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) # 遞迴右子陣列
|
||||
# 合併階段
|
||||
|
|
|
@ -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); // 遞迴右子陣列
|
||||
|
||||
|
|
|
@ -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) // 遞迴右子陣列
|
||||
// 合併階段
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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); // 遞迴右子陣列
|
||||
// 合併階段
|
||||
|
|
|
@ -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); // 遞迴右子陣列
|
||||
// 合併階段
|
||||
|
|
|
@ -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]);
|
||||
// 遞迴建立左子樹和右子樹
|
||||
|
|
Loading…
Reference in a new issue