#include <iostream>
using namespace std;
#define dim(x) (sizeof(x) / sizeof(x[0]))
//二分查找(折半查找)
int BinSearch(int a[], int n, int x)
{
int mid, low = 0, high = n - 1;
while(low <= high)
{
mid = (low + high)/2;
if (x == a[mid])
{
cout << a[mid] << "==" << x << endl;
return mid;
}
else if(x < a[mid])
{
cout << a[mid] << ">" << x << endl;
high = mid - 1;
}
else
{
cout << a[mid] << "<" << x << endl;
low = mid + 1;
}
}
return 0;
}
本内容试读结束,登录后可阅读更多
下载后可阅读完整内容,剩余1页未读,立即下载