Retranslate the complete English edition from Simplified Chinese (#2010)

* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
This commit is contained in:
Yudong Jin
2026-10-31 03:35:51 +08:00
committed by GitHub
parent ef46e2ed95
commit 14c314c08c
1502 changed files with 29459 additions and 23517 deletions

View File

@@ -4,57 +4,57 @@
* Author: JoseHung (szhong@link.cuhk.edu.hk)
*/
/* Binary search (closed interval on both sides) */
/* Binary search (closed interval) */
function binarySearch(nums, target) {
// Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array
// Initialize the closed interval [0, n-1], with i and j pointing to the first and last elements
let i = 0,
j = nums.length - 1;
// Loop, exit when the search interval is empty (empty when i > j)
// Loop until the search interval is empty (when i > j)
while (i <= j) {
// Calculate midpoint index m, use parseInt() to round down
// Calculate midpoint index m, using parseInt() to round down
const m = parseInt(i + (j - i) / 2);
if (nums[m] < target)
// This means target is in the interval [m+1, j]
// In this case, target must be in [m+1, j] if it exists
i = m + 1;
else if (nums[m] > target)
// This means target is in the interval [i, m-1]
// In this case, target must be in [i, m-1] if it exists
j = m - 1;
else return m; // Found the target element, return its index
else return m; // The target element was found; return its index
}
// Target element not found, return -1
// The target element was not found; return -1
return -1;
}
/* Binary search (left-closed right-open interval) */
/* Binary search (left-closed, right-open interval) */
function binarySearchLCRO(nums, target) {
// Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1
// Initialize the left-closed, right-open interval [0, n), with i at the first element and j one past the last
let i = 0,
j = nums.length;
// Loop, exit when the search interval is empty (empty when i = j)
// Loop until the search interval is empty (when i = j)
while (i < j) {
// Calculate midpoint index m, use parseInt() to round down
// Calculate midpoint index m, using parseInt() to round down
const m = parseInt(i + (j - i) / 2);
if (nums[m] < target)
// This means target is in the interval [m+1, j)
// In this case, target must be in [m+1, j) if it exists
i = m + 1;
else if (nums[m] > target)
// This means target is in the interval [i, m)
// In this case, target must be in [i, m) if it exists
j = m;
// Found the target element, return its index
// The target element was found; return its index
else return m;
}
// Target element not found, return -1
// The target element was not found; return -1
return -1;
}
/* Driver Code */
/* Driver code */
const target = 6;
const nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
/* Binary search (closed interval on both sides) */
/* Binary search (closed interval) */
let index = binarySearch(nums, target);
console.log('Index of target element 6 = ' + index);
/* Binary search (left-closed right-open interval) */
/* Binary search (left-closed, right-open interval) */
index = binarySearchLCRO(nums, target);
console.log('Index of target element 6 = ' + index);