#include <iostream>
#define lm(m) sizeof(m)/sizeof(*m)
using namespace std;
int main()
{
int a[11] = { -89, -2, 5, 10, 32, 556, 895, 1000, 1001, 1032, 98453 };
int left = -1;
int right = lm(a);
int mid, key = 895;
while (left < right - 1)
{
mid = (left + right) / 2;
if (a[mid] < key)
{
left = mid;
}
else
{
right = mid;
}
}
if (a[right] == key)
{
cout << "The index of " << key << " is " << right << endl;
}
else
{
cout << "No " << key << " found.\n";
}
system("pause");
}
Comments
Leave a comment