c++实现折半查找算法
时间: 2023-12-07 08:40:05 浏览: 108
以下是C++实现折半查找算法的代码示例:
```c++
#include <iostream>
using namespace std;
int binarySearch(int arr[], int left, int right, int x) {
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == x) {
return mid;
}
if (arr[mid] < x) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
if (result == -1) {
cout << "Element is not present in array" << endl;
} else {
cout << "Element is present at index " << result << endl;
}
return 0;
}
```
阅读全文