This commit is contained in:
krahets
2024-05-24 16:12:17 +08:00
parent e434a3343c
commit 6bac0db1c4
22 changed files with 635 additions and 99 deletions

View File

@@ -83,7 +83,7 @@ In the implementation code, we declare a recursive function `dfs()` to solve the
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);
@@ -114,7 +114,7 @@ In the implementation code, we declare a recursive function `dfs()` to solve the
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);

View File

@@ -63,7 +63,7 @@ The code is as follows:
# 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:
@@ -202,7 +202,7 @@ We can implement a binary search algorithm with the same functionality based on
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:

View File

@@ -35,7 +35,7 @@ Therefore, at the end of the binary, it is certain that: $i$ points to the first
"""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:
@@ -217,7 +217,7 @@ Even so, we can still keep the conditions expanded, as their logic is clearer an
"""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:

View File

@@ -99,7 +99,7 @@ The implementation of merge sort is shown in the following code. Note that the i
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
@@ -142,7 +142,7 @@ The implementation of merge sort is shown in the following code. Note that the i
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
@@ -186,7 +186,7 @@ The implementation of merge sort is shown in the following code. Note that the i
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