#include <iostream>
#include <string>
using namespace std;
int MidSearch(int data[],int low,int high,int x){
int length = sizeof( data ) / sizeof( *data );
int mid;
while(low<=high){
mid = (low+high)/2;
if(data[mid] == x){
return mid;
}else if(data[mid]>x){
high = mid-1;
}else{
low = mid+1;
}
}
}
int main(int argc, char *argv[])
{
int data[] = {1,5,3,9,21,4,7,1,4};
int low = 0;
int high = 8;
int x = 9;
int y;
y = MidSearch(data,low,high,x);
cout<<y;
cout<<"Hello C-Free!"<<endl;
return 0;
}
二分查找
最新推荐文章于 2024-06-11 09:15:00 发布