#include<iostream>#include<algorithm>usingnamespace std;//自己写typedefstructLNode{
LNode *left;
LNode *right;int Length;int Element[50];}LNode,*Linklist;intBinartSearch(Linklist Tbl,int k){int left, right, mid, NoFound =-1;
left =1;
right = Tbl->Length;while(left <= right){
mid =(left + right)/2;if(k < Tbl->Element[mid])
right = mid -1;elseif(k > Tbl->Element[mid])
left = mid +1;elsereturn mid;}return NoFound;}//stlint Elem[50];int a =binary_search(Elem, Elem +50,5);intmain(){int a[100]={4,10,11,30,69,70,96,100};int b =binary_search(a, a +9,4);//查找成功,返回1
cout <<"在数组中查找元素4,结果为:"<< b << endl;int c =binary_search(a, a +9,40);//查找失败,返回0
cout <<"在数组中查找元素40,结果为:"<< c << endl;int d =lower_bound(a, a +9,10)- a;
cout <<"在数组中查找第一个大于等于10的元素位置,结果为:"<< d << endl;int e =lower_bound(a, a +9,101)- a;
cout <<"在数组中查找第一个大于等于101的元素位置,结果为:"<< e << endl;int f =upper_bound(a, a +9,10)- a;
cout <<"在数组中查找第一个大于10的元素位置,结果为:"<< f << endl;int g =upper_bound(a, a +9,101)- a;
cout <<"在数组中查找第一个大于101的元素位置,结果为:"<< g << endl;}