1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <iostream>
using namespace std;
int BinarySearch(int list[], int low, int high, int key);
int main()
{
int Array[] = {1, 3, 4, 6, 9, 10, 14, 19, 23, 30};
int first = 0;
int last = 9;
int value;
for( int i= 0; i< 10; i++)
{
cout << "{" <<Array[i] <<" "<<"}";
}
cout << "\n\nPlease enter a number to find from the above list of number. \n";
cin >> value;
cout << "\n\n";
int Location = BinarySearch(Array, first, last, value);
if(Location==-1)
cout<< "Number is not found.\n";
else
cout<< "Number is found.\n";
return 0;
}
int BinarySearch(int list[], int low, int high, int key)
{
int mid=(high + low)/2;
if(low<=high)
{
if (list[mid] == key)
{
return mid;
}
else if (list[mid] >key)
return BinarySearch(list,low,mid -1, key);
else
return BinarySearch(list,high,mid + 1, key);
}
else return -1;
}
|