mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +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:
@ -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);
|
||||
@ -43,4 +43,4 @@ int main() {
|
||||
cout << "Index of target element 6 =" << index << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user