If Any Required Program Please Ask In Comment I Will Help You(any Program in JAVA or C++) . . THANKS FOR VISITING MY BLOG!

If U LIKE MY PROFILE RAISE YOUR HAND IF U NOT RAISE UR STANDARD. Powered by Blogger.

Sunday, October 21, 2018

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