#include<iostream>
#include<ctime>
using namespace std;
int Find(int s[], int s_num, int n) //对分查找
{
int left, right, mid;
left = 0;
right = n - 1;
while (left <= right)
{
mid = (left + right) / 2;
if (s[mid] < s_num)
left = mid + 1;
else if (s[mid] > s_num)
right = mid - 1;
else
return mid;
}
return -1;
}
int main()
{
int s[1000];
clock_t t_0;
for (int i = 1; i < 1000; i++)
s[i] = i;
t_0 = clock();
int s_bit = Find(s, 200, 1000);
cout << "s_bit = "<<s_bit << endl;
t_0 = clock() - t_0;
cout << t_0 << "毫秒" << endl;
cin.get();
return 0;
}
【算法】对分查找(排序队列)
最新推荐文章于 2021-09-27 09:16:39 发布