#include <iostream>
using std::cout;
using std::endl;
int BinarySearch(int arr[], int len, int target)
{
int first = 0;
int last = len - 1;
int mid;
while (first <= last)
{
mid = (first + last) ⁄ 2;
if (target == arr[mid])
{
return mid;
}
else
{
if (target < arr[mid])
{
last = mid - 1;
}
else
{
first = mid + 1;
}
}
}
return -1;
}
int main(void)
{
int arr[] = { 1, 3, 5, 7, 9 };
int index = BinarySearch(arr, sizeof(arr) ⁄ sizeof(arr[0]), 7);
if (index == -1)
{
cout << "Search Failed" << endl;
}
else
{
cout << "Target Save Index : " << index << endl;
}
index = BinarySearch(arr, sizeof(arr) ⁄ sizeof(arr[0]), 4);
if (index == -1)
{
cout << "Search Failed" << endl;
}
else
{
cout << "Target Save Index : " << index << endl;
}
}