binarysearch.cpp
来自「function to compute an expression using 」· C++ 代码 · 共 28 行
CPP
28 行
// binary search
#include <iostream>
#include <algorithm>
using namespace std;
template<class T>
int binarySearch(T a[], int n, const T& x)
{// Search a[0] <= a[1] <= ... <= a[n-1] for x.
// Return position if found; return -1 otherwise.
int left = 0; // left end of segment
int right = n - 1; // right end of segment
while (left <= right) {
int middle = (left + right)/2; // middle of segment
if (x == a[middle]) return middle;
if (x > a[middle]) left = middle + 1;
else right = middle - 1;
}
return -1; // x not found
}
int main()
{
int a[7] = {0, 2, 3, 4, 6, 7, 9};
cout << binarySearch(a, 7, 4) << endl;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?