Binary Search C++

Binary Search Using C++



========================================================================


#include<iostream>
using namespace std;
main()
{
int Arr[10]={10,20,30,40,50,60,70,80,90,100};
int BEG=0,END=9;//declare and inilialize begin and end
int MID=int ((BEG+END/2));// calculate mid
int item,loc=0;
cout<<"Enter Item to search\n";
cin>>item;
while(BEG<END && Arr[MID]!=item)//while condition
{
if(item<Arr[MID])
{
END=MID-1;
}
else
{
BEG=MID+1;
}

  MID=int ((BEG+END)/2);//new value of mid after while condition apply
}

if(Arr[MID]==item)
{

cout<<item<<"\tFound on idex # "<<MID;

}
else
cout<<"Search unsuccesful\n";
}


OutPut:





=======================================================================


Code By: Kabeer Hussain
SHARE
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment