二分查找

#include <iostream>
using namespace std;
const int SIZE=20;
int binary_search(int arr[], int n, int k);
int main()
{
int d[SIZE];
int index, key, num=0;
cin>>key;
while(cin>>d[num])
num++;
index = binary_search(d, num, key);
if(index >= 0)
cout<<index<<endl;
else
cout<<"NO"<<endl;
return 0;
}
//提交全部程序代码
int binary_search(int arr[], int n, int k)
{
int i=-1;
int low=_____(1)_____,high=_____(2)_____,mid;
while(low<=high)
{
mid=______(3)________;
if(arr[mid]==k)
{
i=mid;
break;
}
else if(_____(4)_____)
high=mid-1;
else
_______(5)__________;
}
return i;
}

``````45
23 36 45 76 81 88 89 91``````

``2``

``````program p1000(Input,Output);
var
a,b,i,mark:integer;
begin
while not eof(input) do
begin
if b=a then
mark := i;
i := i+1;
end;
if mark = 0 then
write('NO')
else
write(mark);
end.
``````

``````#include <iostream>
using namespace std;
const int SIZE=20;
int binary_search(int arr[], int n, int k);
int main()
{
int d[SIZE];
int index, key, num=0;
cin>>key;
while(cin>>d[num])num++;
index = binary_search(d, num, key);
if(index >= 0)
cout<<index<<endl;
else
cout<<"NO"<<endl;
return 0;
}

int binary_search(int arr[], int n, int k)
{
int i=-1;
int low=0,high=n-1,mid;
while(low<=high)
{
mid=(low+high)/2;
if(arr[mid]==k)
{
i=mid;
break;
}
else if(arr[mid]>k)
high=mid-1;
else
low=mid+1;
}
return i;
}``````