折半查找
本博客不再更新,很多其它精彩内容请訪问我的独立博客
1.折半查找的非递归算法
int BinarySearch(SeqList &L,DataType x) { int high=L.n-1,low=0,mid; while(low<=high){ mid=(low+high)/2; if(x.key>L.data[mid].key) low=mid+1; else if(x.key<L.data[mid].key high=mid-1; else return mid; } return -1; };
2.折半查找的递归算法
int BinarySearchRecur(SeqList& L,DataType x,int low,int high){ int mid=-1; if(low<=high){ mid=(low+high)/2; if(x.key>L.data[mid].key) mid=BinarySearchRecur(L,x,mid+1,high); else if(x.key<data[mid].key) mid=BinarySearchRecur(l,x,low,mid-1); } return mid; };