[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:
rongyi 2024-05-18 18:19:19 +08:00 committed by GitHub
parent 0e221540a3
commit 21be3fdaf8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
41 changed files with 57 additions and 59 deletions

View file

@ -43,7 +43,7 @@ void mergeSort(int *nums, int left, int right) {
if (left >= right) if (left >= right)
return; // 当子数组长度为 1 时终止递归 return; // 当子数组长度为 1 时终止递归
// 划分阶段 // 划分阶段
int mid = (left + right) / 2; // 计算中点 int mid = left + (right - left) / 2; // 计算中点
mergeSort(nums, left, mid); // 递归左子数组 mergeSort(nums, left, mid); // 递归左子数组
mergeSort(nums, mid + 1, right); // 递归右子数组 mergeSort(nums, mid + 1, right); // 递归右子数组
// 合并阶段 // 合并阶段

View file

@ -39,7 +39,7 @@ void mergeSort(vector<int> &nums, int left, int right) {
if (left >= right) if (left >= right)
return; // 当子数组长度为 1 时终止递归 return; // 当子数组长度为 1 时终止递归
// 划分阶段 // 划分阶段
int mid = (left + right) / 2; // 计算中点 int mid = left + (right - left) / 2; // 计算中点
mergeSort(nums, left, mid); // 递归左子数组 mergeSort(nums, left, mid); // 递归左子数组
mergeSort(nums, mid + 1, right); // 递归右子数组 mergeSort(nums, mid + 1, right); // 递归右子数组
// 合并阶段 // 合并阶段

View file

@ -39,7 +39,7 @@ public class merge_sort {
// 终止条件 // 终止条件
if (left >= right) return; // 当子数组长度为 1 时终止递归 if (left >= right) return; // 当子数组长度为 1 时终止递归
// 划分阶段 // 划分阶段
int mid = (left + right) / 2; // 计算中点 int mid = left + (right - left) / 2; // 计算中点
MergeSort(nums, left, mid); // 递归左子数组 MergeSort(nums, left, mid); // 递归左子数组
MergeSort(nums, mid + 1, right); // 递归右子数组 MergeSort(nums, mid + 1, right); // 递归右子数组
// 合并阶段 // 合并阶段

View file

@ -36,7 +36,7 @@ void mergeSort(List<int> nums, int left, int right) {
// //
if (left >= right) return; // 1 if (left >= right) return; // 1
// //
int mid = (left + right) ~/ 2; // int mid = left + (right - left) ~/ 2; //
mergeSort(nums, left, mid); // mergeSort(nums, left, mid); //
mergeSort(nums, mid + 1, right); // mergeSort(nums, mid + 1, right); //
// //

View file

@ -46,7 +46,7 @@ func mergeSort(nums []int, left, right int) {
return return
} }
// 划分阶段 // 划分阶段
mid := (left + right) / 2 mid := left + (right - left) / 2
mergeSort(nums, left, mid) mergeSort(nums, left, mid)
mergeSort(nums, mid+1, right) mergeSort(nums, mid+1, right)
// 合并阶段 // 合并阶段

View file

@ -42,7 +42,7 @@ public class merge_sort {
if (left >= right) if (left >= right)
return; // 当子数组长度为 1 时终止递归 return; // 当子数组长度为 1 时终止递归
// 划分阶段 // 划分阶段
int mid = (left + right) / 2; // 计算中点 int mid = left + (right - left) / 2; // 计算中点
mergeSort(nums, left, mid); // 递归左子数组 mergeSort(nums, left, mid); // 递归左子数组
mergeSort(nums, mid + 1, right); // 递归右子数组 mergeSort(nums, mid + 1, right); // 递归右子数组
// 合并阶段 // 合并阶段

View file

@ -39,7 +39,7 @@ function mergeSort(nums, left, right) {
// 终止条件 // 终止条件
if (left >= right) return; // 当子数组长度为 1 时终止递归 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, left, mid); // 递归左子数组
mergeSort(nums, mid + 1, right); // 递归右子数组 mergeSort(nums, mid + 1, right); // 递归右子数组
// 合并阶段 // 合并阶段

View file

@ -40,7 +40,7 @@ fun mergeSort(nums: IntArray, left: Int, right: Int) {
// 终止条件 // 终止条件
if (left >= right) return // 当子数组长度为 1 时终止递归 if (left >= right) return // 当子数组长度为 1 时终止递归
// 划分阶段 // 划分阶段
val mid = (left + right) / 2 // 计算中点 val mid = left + (right - left) / 2 // 计算中点
mergeSort(nums, left, mid) // 递归左子数组 mergeSort(nums, left, mid) // 递归左子数组
mergeSort(nums, mid + 1, right) // 递归右子数组 mergeSort(nums, mid + 1, right) // 递归右子数组
// 合并阶段 // 合并阶段

View file

@ -45,7 +45,7 @@ def merge_sort(nums, left, right)
# 当子数组长度为 1 时终止递归 # 当子数组长度为 1 时终止递归
return if left >= right return if left >= right
# 划分阶段 # 划分阶段
mid = (left + right) / 2 # 计算中点 mid = left + (right - left) / 2 # 计算中点
merge_sort(nums, left, mid) # 递归左子数组 merge_sort(nums, left, mid) # 递归左子数组
merge_sort(nums, mid + 1, right) # 递归右子数组 merge_sort(nums, mid + 1, right) # 递归右子数组
# 合并阶段 # 合并阶段

View file

@ -10,7 +10,7 @@ fn dfs(nums: &[i32], target: i32, i: i32, j: i32) -> i32 {
if i > j { if i > j {
return -1; return -1;
} }
let m: i32 = (i + j) / 2; let m: i32 = i + (j - i) / 2;
if nums[m as usize] < target { if nums[m as usize] < target {
// 递归子问题 f(m+1, j) // 递归子问题 f(m+1, j)
return dfs(nums, target, m + 1, j); return dfs(nums, target, m + 1, j);

View file

@ -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, left, mid); // 递归左子数组
merge_sort(nums, mid + 1, right); // 递归右子数组 merge_sort(nums, mid + 1, right); // 递归右子数组

View file

@ -35,9 +35,7 @@ fn counting_sort_digit(nums: &mut [i32], exp: i32) {
counter[d] -= 1; // 将 d 的数量减 1 counter[d] -= 1; // 将 d 的数量减 1
} }
// 使用结果覆盖原数组 nums // 使用结果覆盖原数组 nums
for i in 0..n { nums.copy_from_slice(&res);
nums[i] = res[i];
}
} }
/* 基数排序 */ /* 基数排序 */

View file

@ -46,7 +46,7 @@ func mergeSort(nums: inout [Int], left: Int, right: Int) {
return return
} }
// //
let mid = (left + right) / 2 // let mid = left + (right - left) / 2 //
mergeSort(nums: &nums, left: left, right: mid) // mergeSort(nums: &nums, left: left, right: mid) //
mergeSort(nums: &nums, left: mid + 1, right: right) // mergeSort(nums: &nums, left: mid + 1, right: right) //
// //

View file

@ -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 { 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) nums.swapAt(left, med)
return partition(nums: &nums, left: left, right: right) return partition(nums: &nums, left: left, right: right)

View file

@ -39,7 +39,7 @@ function mergeSort(nums: number[], left: number, right: number): void {
// 终止条件 // 终止条件
if (left >= right) return; // 当子数组长度为 1 时终止递归 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, left, mid); // 递归左子数组
mergeSort(nums, mid + 1, right); // 递归右子数组 mergeSort(nums, mid + 1, right); // 递归右子数组
// 合并阶段 // 合并阶段

View file

@ -48,7 +48,7 @@ fn mergeSort(nums: []i32, left: usize, right: usize) !void {
// //
if (left >= right) return; // 1 if (left >= right) return; // 1
// //
var mid = (left + right) / 2; // var mid = left + (right - left) / 2; //
try mergeSort(nums, left, mid); // try mergeSort(nums, left, mid); //
try mergeSort(nums, mid + 1, right); // try mergeSort(nums, mid + 1, right); //
// //

View file

@ -34,7 +34,7 @@ pub fn BinarySearchTree(comptime T: type) type {
fn buildTree(self: *Self, nums: []T, i: usize, j: usize) !?*inc.TreeNode(T) { fn buildTree(self: *Self, nums: []T, i: usize, j: usize) !?*inc.TreeNode(T) {
if (i > j) return null; 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)); var node = try self.mem_allocator.create(inc.TreeNode(T));
node.init(nums[mid]); node.init(nums[mid]);
// //

View file

@ -13,7 +13,7 @@ int dfs(vector<int> &nums, int target, int i, int j) {
return -1; return -1;
} }
// Calculate midpoint index m // Calculate midpoint index m
int m = (i + j) / 2; int m = i + (j - i) / 2;
if (nums[m] < target) { if (nums[m] < target) {
// Recursive subproblem f(m+1, j) // Recursive subproblem f(m+1, j)
return dfs(nums, target, m + 1, j); return dfs(nums, target, m + 1, j);

View file

@ -39,7 +39,7 @@ void mergeSort(vector<int> &nums, int left, int right) {
if (left >= right) if (left >= right)
return; // Terminate recursion when subarray length is 1 return; // Terminate recursion when subarray length is 1
// Partition stage // 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, left, mid); // Recursively process the left subarray
mergeSort(nums, mid + 1, right); // Recursively process the right subarray mergeSort(nums, mid + 1, right); // Recursively process the right subarray
// Merge stage // Merge stage

View file

@ -14,7 +14,7 @@ public class binary_search_recur {
return -1; return -1;
} }
// Calculate midpoint index m // Calculate midpoint index m
int m = (i + j) / 2; int m = i + (j - i) / 2;
if (nums[m] < target) { if (nums[m] < target) {
// Recursive subproblem f(m+1, j) // Recursive subproblem f(m+1, j)
return dfs(nums, target, m + 1, j); return dfs(nums, target, m + 1, j);

View file

@ -42,7 +42,7 @@ public class merge_sort {
if (left >= right) if (left >= right)
return; // Terminate recursion when subarray length is 1 return; // Terminate recursion when subarray length is 1
// Partition stage // 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, left, mid); // Recursively process the left subarray
mergeSort(nums, mid + 1, right); // Recursively process the right subarray mergeSort(nums, mid + 1, right); // Recursively process the right subarray
// Merge stage // Merge stage

View file

@ -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) # Loop until the search interval is empty (when i > j, it is empty)
while i <= j: 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 # 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: if nums[m] < target:
i = m + 1 # This situation indicates that target is in the interval [m+1, j] i = m + 1 # This situation indicates that target is in the interval [m+1, j]
elif nums[m] > target: elif nums[m] > target:
@ -28,7 +28,7 @@ def binary_search_lcro(nums: list[int], target: int) -> int:
i, j = 0, len(nums) i, j = 0, len(nums)
# Loop until the search interval is empty (when i = j, it is empty) # Loop until the search interval is empty (when i = j, it is empty)
while i < j: while i < j:
m = (i + j) // 2 # Calculate midpoint index m m = i + (j - i) // 2 # Calculate midpoint index m
if nums[m] < target: if nums[m] < target:
i = m + 1 # This situation indicates that target is in the interval [m+1, j) i = m + 1 # This situation indicates that target is in the interval [m+1, j)
elif nums[m] > target: elif nums[m] > target:

View file

@ -9,7 +9,7 @@ def binary_search_insertion_simple(nums: list[int], target: int) -> int:
"""Binary search for insertion point (no duplicate elements)""" """Binary search for insertion point (no duplicate elements)"""
i, j = 0, len(nums) - 1 # Initialize double closed interval [0, n-1] i, j = 0, len(nums) - 1 # Initialize double closed interval [0, n-1]
while i <= j: while i <= j:
m = (i + j) // 2 # Calculate midpoint index m m = i + (j - i) // 2 # Calculate midpoint index m
if nums[m] < target: if nums[m] < target:
i = m + 1 # Target is in interval [m+1, j] i = m + 1 # Target is in interval [m+1, j]
elif nums[m] > target: 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)""" """Binary search for insertion point (with duplicate elements)"""
i, j = 0, len(nums) - 1 # Initialize double closed interval [0, n-1] i, j = 0, len(nums) - 1 # Initialize double closed interval [0, n-1]
while i <= j: while i <= j:
m = (i + j) // 2 # Calculate midpoint index m m = i + (j - i) // 2 # Calculate midpoint index m
if nums[m] < target: if nums[m] < target:
i = m + 1 # Target is in interval [m+1, j] i = m + 1 # Target is in interval [m+1, j]
elif nums[m] > target: elif nums[m] > target:

View file

@ -41,7 +41,7 @@ def merge_sort(nums: list[int], left: int, right: int):
if left >= right: if left >= right:
return # Terminate recursion when subarray length is 1 return # Terminate recursion when subarray length is 1
# Partition stage # 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, left, mid) # Recursively process the left subarray
merge_sort(nums, mid + 1, right) # Recursively process the right subarray merge_sort(nums, mid + 1, right) # Recursively process the right subarray
# Merge stage # Merge stage

View file

@ -43,7 +43,7 @@ void mergeSort(int *nums, int left, int right) {
if (left >= right) if (left >= right)
return; // 當子陣列長度為 1 時終止遞迴 return; // 當子陣列長度為 1 時終止遞迴
// 劃分階段 // 劃分階段
int mid = (left + right) / 2; // 計算中點 int mid = left + (right - left) / 2; // 計算中點
mergeSort(nums, left, mid); // 遞迴左子陣列 mergeSort(nums, left, mid); // 遞迴左子陣列
mergeSort(nums, mid + 1, right); // 遞迴右子陣列 mergeSort(nums, mid + 1, right); // 遞迴右子陣列
// 合併階段 // 合併階段

View file

@ -39,7 +39,7 @@ void mergeSort(vector<int> &nums, int left, int right) {
if (left >= right) if (left >= right)
return; // 當子陣列長度為 1 時終止遞迴 return; // 當子陣列長度為 1 時終止遞迴
// 劃分階段 // 劃分階段
int mid = (left + right) / 2; // 計算中點 int mid = left + (right - left) / 2; // 計算中點
mergeSort(nums, left, mid); // 遞迴左子陣列 mergeSort(nums, left, mid); // 遞迴左子陣列
mergeSort(nums, mid + 1, right); // 遞迴右子陣列 mergeSort(nums, mid + 1, right); // 遞迴右子陣列
// 合併階段 // 合併階段

View file

@ -39,7 +39,7 @@ public class merge_sort {
// 終止條件 // 終止條件
if (left >= right) return; // 當子陣列長度為 1 時終止遞迴 if (left >= right) return; // 當子陣列長度為 1 時終止遞迴
// 劃分階段 // 劃分階段
int mid = (left + right) / 2; // 計算中點 int mid = left + (right - left) / 2; // 計算中點
MergeSort(nums, left, mid); // 遞迴左子陣列 MergeSort(nums, left, mid); // 遞迴左子陣列
MergeSort(nums, mid + 1, right); // 遞迴右子陣列 MergeSort(nums, mid + 1, right); // 遞迴右子陣列
// 合併階段 // 合併階段

View file

@ -36,7 +36,7 @@ void mergeSort(List<int> nums, int left, int right) {
// //
if (left >= right) return; // 1 if (left >= right) return; // 1
// //
int mid = (left + right) ~/ 2; // int mid = left + (right - left) ~/ 2; //
mergeSort(nums, left, mid); // mergeSort(nums, left, mid); //
mergeSort(nums, mid + 1, right); // mergeSort(nums, mid + 1, right); //
// //

View file

@ -46,7 +46,7 @@ func mergeSort(nums []int, left, right int) {
return return
} }
// 劃分階段 // 劃分階段
mid := (left + right) / 2 mid := left + (right - left) / 2
mergeSort(nums, left, mid) mergeSort(nums, left, mid)
mergeSort(nums, mid+1, right) mergeSort(nums, mid+1, right)
// 合併階段 // 合併階段

View file

@ -42,7 +42,7 @@ public class merge_sort {
if (left >= right) if (left >= right)
return; // 當子陣列長度為 1 時終止遞迴 return; // 當子陣列長度為 1 時終止遞迴
// 劃分階段 // 劃分階段
int mid = (left + right) / 2; // 計算中點 int mid = left + (right - left) / 2; // 計算中點
mergeSort(nums, left, mid); // 遞迴左子陣列 mergeSort(nums, left, mid); // 遞迴左子陣列
mergeSort(nums, mid + 1, right); // 遞迴右子陣列 mergeSort(nums, mid + 1, right); // 遞迴右子陣列
// 合併階段 // 合併階段

View file

@ -39,7 +39,7 @@ function mergeSort(nums, left, right) {
// 終止條件 // 終止條件
if (left >= right) return; // 當子陣列長度為 1 時終止遞迴 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, left, mid); // 遞迴左子陣列
mergeSort(nums, mid + 1, right); // 遞迴右子陣列 mergeSort(nums, mid + 1, right); // 遞迴右子陣列
// 合併階段 // 合併階段

View file

@ -40,7 +40,7 @@ fun mergeSort(nums: IntArray, left: Int, right: Int) {
// 終止條件 // 終止條件
if (left >= right) return // 當子陣列長度為 1 時終止遞迴 if (left >= right) return // 當子陣列長度為 1 時終止遞迴
// 劃分階段 // 劃分階段
val mid = (left + right) / 2 // 計算中點 val mid = left + (right - left) / 2 // 計算中點
mergeSort(nums, left, mid) // 遞迴左子陣列 mergeSort(nums, left, mid) // 遞迴左子陣列
mergeSort(nums, mid + 1, right) // 遞迴右子陣列 mergeSort(nums, mid + 1, right) // 遞迴右子陣列
// 合併階段 // 合併階段

View file

@ -41,7 +41,7 @@ def merge_sort(nums: list[int], left: int, right: int):
if left >= right: if left >= right:
return # 當子陣列長度為 1 時終止遞迴 return # 當子陣列長度為 1 時終止遞迴
# 劃分階段 # 劃分階段
mid = (left + right) // 2 # 計算中點 mid = left + (right - left) // 2 # 計算中點
merge_sort(nums, left, mid) # 遞迴左子陣列 merge_sort(nums, left, mid) # 遞迴左子陣列
merge_sort(nums, mid + 1, right) # 遞迴右子陣列 merge_sort(nums, mid + 1, right) # 遞迴右子陣列
# 合併階段 # 合併階段

View file

@ -45,7 +45,7 @@ def merge_sort(nums, left, right)
# 當子陣列長度為 1 時終止遞迴 # 當子陣列長度為 1 時終止遞迴
return if left >= right return if left >= right
# 劃分階段 # 劃分階段
mid = (left + right) / 2 # 計算中點 mid = left + (right - left) / 2 # 計算中點
merge_sort(nums, left, mid) # 遞迴左子陣列 merge_sort(nums, left, mid) # 遞迴左子陣列
merge_sort(nums, mid + 1, right) # 遞迴右子陣列 merge_sort(nums, mid + 1, right) # 遞迴右子陣列
# 合併階段 # 合併階段

View file

@ -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, left, mid); // 遞迴左子陣列
merge_sort(nums, mid + 1, right); // 遞迴右子陣列 merge_sort(nums, mid + 1, right); // 遞迴右子陣列

View file

@ -46,7 +46,7 @@ func mergeSort(nums: inout [Int], left: Int, right: Int) {
return return
} }
// //
let mid = (left + right) / 2 // let mid = left + (right - left) / 2 //
mergeSort(nums: &nums, left: left, right: mid) // mergeSort(nums: &nums, left: left, right: mid) //
mergeSort(nums: &nums, left: mid + 1, right: right) // mergeSort(nums: &nums, left: mid + 1, right: right) //
// //

View file

@ -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 { 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) nums.swapAt(left, med)
return partition(nums: &nums, left: left, right: right) return partition(nums: &nums, left: left, right: right)

View file

@ -39,7 +39,7 @@ function mergeSort(nums: number[], left: number, right: number): void {
// 終止條件 // 終止條件
if (left >= right) return; // 當子陣列長度為 1 時終止遞迴 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, left, mid); // 遞迴左子陣列
mergeSort(nums, mid + 1, right); // 遞迴右子陣列 mergeSort(nums, mid + 1, right); // 遞迴右子陣列
// 合併階段 // 合併階段

View file

@ -48,7 +48,7 @@ fn mergeSort(nums: []i32, left: usize, right: usize) !void {
// //
if (left >= right) return; // 1 if (left >= right) return; // 1
// //
var mid = (left + right) / 2; // var mid = left + (right - left) / 2; //
try mergeSort(nums, left, mid); // try mergeSort(nums, left, mid); //
try mergeSort(nums, mid + 1, right); // try mergeSort(nums, mid + 1, right); //
// //

View file

@ -34,7 +34,7 @@ pub fn BinarySearchTree(comptime T: type) type {
fn buildTree(self: *Self, nums: []T, i: usize, j: usize) !?*inc.TreeNode(T) { fn buildTree(self: *Self, nums: []T, i: usize, j: usize) !?*inc.TreeNode(T) {
if (i > j) return null; 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)); var node = try self.mem_allocator.create(inc.TreeNode(T));
node.init(nums[mid]); node.init(nums[mid]);
// //