Skip to content

Commit

Permalink
Merge pull request srbcheema1#32 from Balaji2198/master
Browse files Browse the repository at this point in the history
Adding Binary Search in cpp
  • Loading branch information
srbcheema1 authored Oct 1, 2017
2 parents 4801a75 + d1b4950 commit 59f4610
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Searching Algorithms/Binary_Search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include<bits/stdc++.h>
using namespace std;
int main()
{

int a[100],n,i,beg,end,mid,item;
cout<<"Enter No. of Elements= ";
cin>>n;
cout<<"Enter Elements in Sorted Order=\n";
for(i=1;i<=n;i++)
cin>>a[i];

cout<<"Enter Item you want to Search= ";
cin>>item;

beg=1;
end=n;

mid=(beg+end)/2;

while(beg<=end && a[mid]!=item)
{
if(a[mid]<item)
beg=mid+1;
else
end=mid-1;
mid=(beg+end)/2;
}
if(a[mid]==item)
cout<<"Data is Found at Location : "<<mid;
else
cout<<"Data is Not Found";

return 0;
}

0 comments on commit 59f4610

Please sign in to comment.