Skip to content

Commit b1aa420

Browse files
Hrishikesh-ThakkarrootAadit Kamat
authored
Added Binary Search Implementation to Day29 in C++ (#340)
* Added C++ Implementation to Day29 * Add @Hrishikesh-Thakkar as a contributor * Updated Changes to comment removed unnecessary sort function Co-authored-by: root <[email protected]> Co-authored-by: Aadit Kamat <[email protected]>
1 parent 8f5f8f6 commit b1aa420

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,15 @@
289289
"doc",
290290
"code"
291291
]
292+
},
293+
{
294+
"login": "Hrishikesh-Thakkar",
295+
"name": "Hrishikesh K Thakkar",
296+
"avatar_url": "https://avatars0.githubusercontent.com/u/25359325?v=4",
297+
"profile": "https://github.com/Hrishikesh-Thakkar",
298+
"contributions": [
299+
"code"
300+
]
292301
}
293302
]
294303
}

CONTRIBUTORS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![All Contributors](https://img.shields.io/badge/all_contributors-28-orange.svg?style=flat-square)](#contributors)
1+
[![All Contributors](https://img.shields.io/badge/all_contributors-29-orange.svg?style=flat-square)](#contributors)
22

33
## Contributors
44

@@ -9,7 +9,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
99
| [<img src="https://avatars2.githubusercontent.com/u/26179770?v=4" width="100px;"/><br /><sub><b>MADHAV BAHL</b></sub>](http://madhavbahl.tech/)<br />[💻](https://github.com/CodeToExpress/dailycodebase/commits?author=MadhavBahlMD "Code") [📝](#blog-MadhavBahlMD "Blogposts") [📖](https://github.com/CodeToExpress/dailycodebase/commits?author=MadhavBahlMD "Documentation") [🎨](#design-MadhavBahlMD "Design") [✅](#tutorial-MadhavBahlMD "Tutorials") | [<img src="https://avatars2.githubusercontent.com/u/32531173?v=4" width="100px;"/><br /><sub><b>Rajdeep Roy Chowdhury</b></sub>](http://www.linkedin.com/in/razdeeproychowdhury)<br />[💻](https://github.com/CodeToExpress/dailycodebase/commits?author=Razdeep "Code") [📖](https://github.com/CodeToExpress/dailycodebase/commits?author=Razdeep "Documentation") [✅](#tutorial-Razdeep "Tutorials") | [<img src="https://avatars1.githubusercontent.com/u/16208722?v=4" width="100px;"/><br /><sub><b>Hrishi</b></sub>](https://hrishi1999.github.io)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=Hrishi1999 "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=Hrishi1999 "Code") | [<img src="https://avatars1.githubusercontent.com/u/33101287?v=4" width="100px;"/><br /><sub><b>Bhanu Ailani</b></sub>](https://github.com/Bhanu0202)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=Bhanu0202 "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=Bhanu0202 "Code") | [<img src="https://avatars3.githubusercontent.com/u/35690440?v=4" width="100px;"/><br /><sub><b>Deepak Sharma</b></sub>](https://sourcerer.io/dsdsharma)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=dsdsharma "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=dsdsharma "Code") | [<img src="https://avatars2.githubusercontent.com/u/28749866?v=4" width="100px;"/><br /><sub><b>Ramanpreet Singh</b></sub>](https://github.com/Ramanpreet6262)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=Ramanpreet6262 "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=Ramanpreet6262 "Code") | [<img src="https://avatars1.githubusercontent.com/u/27620628?v=4" width="100px;"/><br /><sub><b>Anil Khatri</b></sub>](https://imkaka.github.io/)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=imkaka "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=imkaka "Code") |
1010
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
1111
| [<img src="https://avatars0.githubusercontent.com/u/39827514?v=4" width="100px;"/><br /><sub><b>Ashwek Swamy</b></sub>](https://github.com/ashwek)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=ashwek "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=ashwek "Code") | [<img src="https://avatars1.githubusercontent.com/u/32368107?v=4" width="100px;"/><br /><sub><b>Dhruv Gupta</b></sub>](https://github.com/dhruv-gupta14)<br />[💻](https://github.com/CodeToExpress/dailycodebase/commits?author=dhruv-gupta14 "Code") | [<img src="https://avatars0.githubusercontent.com/u/30969577?v=4" width="100px;"/><br /><sub><b>Aadit Kamat</b></sub>](https://sg.linkedin.com/in/aadit-kamat-12a5a8140)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=aaditkamat "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=aaditkamat "Code") | [<img src="https://avatars1.githubusercontent.com/u/20285786?v=4" width="100px;"/><br /><sub><b>Shashank jain</b></sub>](http://www.myshashank.tech)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=Shashankjain12 "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=Shashankjain12 "Code") | [<img src="https://avatars3.githubusercontent.com/u/22260031?v=4" width="100px;"/><br /><sub><b>Moulik Aggarwal</b></sub>](https://github.com/moulikcipherX)<br />[💬](#question-moulikcipherX "Answering Questions") [📝](#blog-moulikcipherX "Blogposts") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=moulikcipherX "Code") [📖](https://github.com/CodeToExpress/dailycodebase/commits?author=moulikcipherX "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/21277179?v=4" width="100px;"/><br /><sub><b>Prateek Surana</b></sub>](http://prateeksurana.me)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=prateek3255 "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=prateek3255 "Code") | [<img src="https://avatars2.githubusercontent.com/u/43927436?v=4" width="100px;"/><br /><sub><b>shivank86</b></sub>](https://github.com/shivank86)<br />[💻](https://github.com/CodeToExpress/dailycodebase/commits?author=shivank86 "Code") |
12-
| [<img src="https://avatars2.githubusercontent.com/u/25405726?v=4" width="100px;"/><br /><sub><b>Vishal Shirke</b></sub>](https://github.com/vishalshirke7)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=vishalshirke7 "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=vishalshirke7 "Code") | [<img src="https://avatars1.githubusercontent.com/u/25369498?v=4" width="100px;"/><br /><sub><b>profgrammer</b></sub>](https://github.com/profgrammer)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=profgrammer "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=profgrammer "Code") | [<img src="https://avatars1.githubusercontent.com/u/26656036?v=4" width="100px;"/><br /><sub><b>divyakhetan</b></sub>](https://github.com/divyakhetan)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=divyakhetan "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=divyakhetan "Code") | [<img src="https://avatars0.githubusercontent.com/u/36483814?v=4" width="100px;"/><br /><sub><b>Spreeha Dutta</b></sub>](https://github.com/Spreeha)<br />[💻](https://github.com/CodeToExpress/dailycodebase/commits?author=Spreeha "Code") [📖](https://github.com/CodeToExpress/dailycodebase/commits?author=Spreeha "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/26464695?v=4" width="100px;"/><br /><sub><b>Chaitanya Bhojwani</b></sub>](https://github.com/chaitanya-bhojwani)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=chaitanya-bhojwani "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=chaitanya-bhojwani "Code") | [<img src="https://avatars0.githubusercontent.com/u/22885912?v=4" width="100px;"/><br /><sub><b>Manaswini Das</b></sub>](https://www.linkedin.com/in/manaswini-das/)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=manaswinidas "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/25549847?s=460&v=4" width="100px;"/><br /><sub><b>Anirudh Jwala</b></sub>](https://www.linkedin.com/in/anirudh-jwala-533859135/)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=anirudh-jwala "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=anirudh-jwala "Code") |
12+
| [<img src="https://avatars2.githubusercontent.com/u/25405726?v=4" width="100px;"/><br /><sub><b>Vishal Shirke</b></sub>](https://github.com/vishalshirke7)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=vishalshirke7 "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=vishalshirke7 "Code") | [<img src="https://avatars1.githubusercontent.com/u/25369498?v=4" width="100px;"/><br /><sub><b>profgrammer</b></sub>](https://github.com/profgrammer)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=profgrammer "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=profgrammer "Code") | [<img src="https://avatars1.githubusercontent.com/u/26656036?v=4" width="100px;"/><br /><sub><b>divyakhetan</b></sub>](https://github.com/divyakhetan)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=divyakhetan "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=divyakhetan "Code") | [<img src="https://avatars0.githubusercontent.com/u/36483814?v=4" width="100px;"/><br /><sub><b>Spreeha Dutta</b></sub>](https://github.com/Spreeha)<br />[💻](https://github.com/CodeToExpress/dailycodebase/commits?author=Spreeha "Code") [📖](https://github.com/CodeToExpress/dailycodebase/commits?author=Spreeha "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/26464695?v=4" width="100px;"/><br /><sub><b>Chaitanya Bhojwani</b></sub>](https://github.com/chaitanya-bhojwani)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=chaitanya-bhojwani "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=chaitanya-bhojwani "Code") | [<img src="https://avatars0.githubusercontent.com/u/22885912?v=4" width="100px;"/><br /><sub><b>Manaswini Das</b></sub>](https://www.linkedin.com/in/manaswini-das/)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=manaswinidas "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/25549847?s=460&v=4" width="100px;"/><br /><sub><b>Anirudh Jwala</b></sub>](https://www.linkedin.com/in/anirudh-jwala-533859135/)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=anirudh-jwala "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=anirudh-jwala "Code") | | [<img src="https://avatars0.githubusercontent.com/u/25359325?v=4" width="100px;"/><br /><sub><b>Hrishikesh K Thakkar</b></sub>](https://github.com/Hrishikesh-Thakkar)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=Hrishikesh-Thakkar "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=Hrishikesh-Thakkar "Code") |
1313
| [<img src="https://avatars0.githubusercontent.com/u/2049543?v=4" width="100px;"/><br /><sub><b>Willian Taiguara Baliza Moura</b></sub>](https://github.com/willianwt)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=willianwt "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=willianwt "Code") | [<img src="https://avatars3.githubusercontent.com/u/33730790?v=4" width="100px;"/><br /><sub><b>Rajashree Parhi</b></sub>](https://github.com/rajashree23)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=rajashree23 "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=rajashree23 "Code") | [<img src="https://avatars1.githubusercontent.com/u/40717067?v=4" width="100px;"/><br /><sub><b>Akshit Aggarwal</b></sub>](https://github.com/AkshitAggarwal)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=AkshitAggarwal "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=AkshitAggarwal "Code") | [<img src="https://avatars1.githubusercontent.com/u/44455626?v=4" width="100px;"/><br /><sub><b>Abhishek</b></sub>](https://github.com/dasabhishek0790)<br />[💻](https://github.com/CodeToExpress/dailycodebase/commits?author=dasabhishek0790 "Code") | [<img src="https://avatars3.githubusercontent.com/u/63302439?v=4" width="100px;"/><br /><sub><b>SAGUN JAISWAL</b></sub>](https://github.com/sagunjaiswal)<br />[💻](https://github.com/CodeToExpress/dailycodebase/commits?author=sagunjaiswal "Code") | [<img src="https://avatars0.githubusercontent.com/u/55596147?v=4" width="100px;"/><br /><sub><b>Abhishek Anantharam</b></sub>](https://github.com/abhishek0405)<br />[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=abhishek0405 "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=abhishek0405 "Code") | [<img src="https://avatars3.githubusercontent.com/u/26011598?v=4" width="100px;"/><br/><sub><b>Prabhat Malhotra</b></sub>](https://github.com/malhotra1432)<br/>[📖](https://github.com/CodeToExpress/dailycodebase/commits?author=malhotra1432 "Documentation") [💻](https://github.com/CodeToExpress/dailycodebase/commits?author=malhotra1432 "Code") |
1414
<!-- ALL-CONTRIBUTORS-LIST:END -->
1515

day29/C++/kesh5210_binary_search.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @author: Hrishikesh-Thakkar
3+
* @date: 10/1/2020
4+
**/
5+
6+
#include<bits/stdc++.h>
7+
using namespace std;
8+
9+
//Recursive Approach
10+
int binarySearchRecurse(vector<int> &arr,int low,int high,int key){
11+
12+
if(low<=high){
13+
int mid = low + (high-low)/2;
14+
if(arr[mid] == key) {
15+
return mid;
16+
}
17+
else if(arr[mid]<key) {
18+
return binarySearchRecurse(arr,mid+1,high,key);
19+
}
20+
else {
21+
return binarySearchRecurse(arr,low,mid-1,key);
22+
}
23+
}
24+
return -1;
25+
}
26+
27+
//Iterative Approach
28+
int binarySearchIterate(vector<int> &arr,int low, int high, int key){
29+
30+
while(low<=high){
31+
int mid = low + (high-low)/2;
32+
if(arr[mid] == key){
33+
return mid;
34+
}
35+
else if(arr[mid]<key){
36+
low = mid+1;
37+
}
38+
else{
39+
high = mid-1;
40+
}
41+
}
42+
43+
return -1;
44+
}
45+
int main(){
46+
47+
//Input array
48+
vector<int> arr;
49+
for(int i=0;i<=8;i++)
50+
arr.push_back(pow(2,i));
51+
52+
//Number to search for
53+
int key = 32;
54+
55+
//Ensure the Array is sorted as Binary Search works only with Sorted Arrays
56+
57+
cout<<"For the given array: ";
58+
for(int i=0;i<arr.size();i++)
59+
cout<<arr[i]<<" ";
60+
cout<<endl;
61+
cout<<"For the key: "<<key<<endl;
62+
63+
//Returns 0 indexed position of element in sorted array.
64+
cout<<"Binary Search using Recursion:"<<binarySearchRecurse(arr,0,arr.size()-1,key)<<endl;
65+
cout<<"Binary Search using Iteration:"<<binarySearchIterate(arr,0,arr.size()-1,key)<<endl;
66+
return 0;
67+
}
68+
69+
/*
70+
Algorithmic Analysis:
71+
72+
Binary Search Time Complexity is log(n)
73+
This is due to fact that at each comparison if the element is not matched then half of the remaining array is ignored
74+
based on the key and the value of the array we are currently at.
75+
76+
Binary Search takes:
77+
1. Input Array
78+
2. Lower Search Bound
79+
3. Upper Search Bound
80+
4. Number to search for
81+
82+
Returns 0 based index of element if found or -1 if not found.
83+
*/

0 commit comments

Comments
 (0)