PROGRAM FOR BINARY SEARCH USING RECURSION.
#include <iostream>
#include <conio.h>
using namespace std;
int B_Search(int *Arr, int No, int Beg, int End );
int main()
{
int *Array, Value, Size = 10;
Array = new int[Size];
for(int I = 0; I <Size; I++)
Array[I] = I +1;
cout << "Enter Value You Want To Search: ";
cin >> Value;
if(B_Search(Array, Value, 0, Size -1) <0)
cout << "\nValue Not Found!!!! " << endl;
else
cout << "Value Find At Index: " << B_Search(Array, Value, 0, Size -1) << endl;
getch();
return 0;
}
int B_Search(int *Arr, int No, int Beg, int End )
{
int Mid = (Beg + End) /2;
if(Beg >End)
return -1;
else if(Arr[Mid] == No)
return Mid;
else if(Arr[Mid] < No)
B_Search(Arr, No, Mid+1, End);
else
B_Search(Arr, No, Beg, Mid-1);
}
0 comments:
Post a Comment