From 9a84ac43c03a7ef32937a0046a8592682ded4f35 Mon Sep 17 00:00:00 2001 From: pkdism Date: Sun, 20 Nov 2016 12:05:58 +0530 Subject: [PATCH] Add Binary Search --- Binary Search.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Binary Search.cpp diff --git a/Binary Search.cpp b/Binary Search.cpp new file mode 100644 index 000000000..cb67e3a07 --- /dev/null +++ b/Binary Search.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; +int binary_search(int a[],int l,int r,int key){ + while(l<=r){ + int m = l+(r-l)/2; + if(key==a[m]) + return m; + else if(key>n; + cout<<"Enter array elements: "; + int a[n]; + for (int i = 0; i < n; ++i) + { + cin>>a[i]; + } + cout<<"Enter search key: "; + cin>>key; + int res = binary_search(a,0,n-1,key); + if(res != -1) + cout<