From 2057de8f97c91ee40977c2eb315429ac2e74add4 Mon Sep 17 00:00:00 2001 From: Jasna Suresh Date: Wed, 9 Oct 2019 14:33:26 +0530 Subject: [PATCH] Added Binary search in C++ --- binarysearch.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 binarysearch.cpp diff --git a/binarysearch.cpp b/binarysearch.cpp new file mode 100644 index 0000000..6980d1a --- /dev/null +++ b/binarysearch.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; +#include +int binarysearch() +{ +int n,key; +cout<<"enter the size of the array"; +cin>>n; +int a[n]; +cout<<"enter the sorted array"; +for(int i=0;i>a[i]; +cout<<"enter the element to be searched"; +cin>>key; +int beg,end,mid; +beg=0; +end=n-1; +while(beg<=end) +{ +mid=(beg+end)/2; +if(key==a[mid]) +return a[mid]; +else if(key