给我写一段二分查找算法代码
时间: 2023-04-02 18:03:53 浏览: 76
好的,以下是二分查找算法的代码:
int binarySearch(int arr[], int left, int right, int target) {
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
阅读全文