We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
二分搜索:
代码实现如下:
function binarySearch(arr, value) { let low = 0 let high = arr.length - 1 while (low <= high) { const midIndex = Math.floor((low + high) / 2) const mid = arr[midIndex] if (mid < value) { // 说明在mid的左边 low = midIndex + 1 } else if (mid > value) { // 说明在mid的右边 high = midIndex - 1 } else if (mid === value) { // 找到了 return midIndex } } return -1 } const arr = [1, 2, 3, 4, 5, 6, 7] console.log(binarySearch(arr, 7))
The text was updated successfully, but these errors were encountered:
No branches or pull requests
二分搜索:
代码实现如下:
The text was updated successfully, but these errors were encountered: