-
Notifications
You must be signed in to change notification settings - Fork 497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Binary Search [C++] #550
Conversation
Hi! Thanks for showing interest in contributing to this repository. Please make sure that you have ticked the points mentioned in PR description correctly. Thanks again! |
@ishakalra: Please fix travis build. Thanks :) |
@singhpratyush Hi, earlier the build failed due to indent spaces/tabs, I tried modifying that but now I can't seem to figure out where the fault is. Would be really grateful if you could guide me through the issue! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make a new entry in README and see comments
binary_search/binarysearch.cpp
Outdated
@@ -0,0 +1,38 @@ | |||
#include<iostream> | |||
using namespace std; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Give a blank line at line 2 and 3.
binary_search/binarysearch.cpp
Outdated
#include<iostream> | ||
using namespace std; | ||
int binarySearchIterative(int array[], int start, int end, int find) | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow Google's C++ style guide. https://google.github.io/styleguide/cppguide.html
binary_search/binarysearch.cpp
Outdated
{ | ||
while(end >= start) | ||
{ | ||
int mid = start + (end - start)/2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put space around binary operators.
binary_search/binarysearch.cpp
Outdated
} | ||
return -1; | ||
} | ||
int binarySearchRecursive(int array[], int start, int end, int find) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blank line at line 17.
binary_search/binarysearch.cpp
Outdated
int array[] = { 1, 2, 3, 4, 5, 6, 11, 23, 25, 66, 88, 99, 113 }; | ||
int end = sizeof(array)/sizeof(array[0]); | ||
cout<<binarySearchIterative(array,0,end,1)<<'\n'<<binarySearchRecursive(array,0,end,1)<<'\n'; | ||
cout<<binarySearchIterative(array,0,end,0)<<'\n'<<binarySearchRecursive(array,0,end,0)<<'\n'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put space around <<
.
binary_search/binarysearch.cpp
Outdated
int end = sizeof(array)/sizeof(array[0]); | ||
cout<<binarySearchIterative(array,0,end,1)<<'\n'<<binarySearchRecursive(array,0,end,1)<<'\n'; | ||
cout<<binarySearchIterative(array,0,end,0)<<'\n'<<binarySearchRecursive(array,0,end,0)<<'\n'; | ||
return 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ishakalra Try returning 0
here, let's see if the build is fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make the following changes.
binary_search/binarysearch.cpp
Outdated
@@ -0,0 +1,38 @@ | |||
#include<iostream> | |||
using namespace std; | |||
int binarySearchIterative(int array[], int start, int end, int find) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use underscores for naming variables and functions instead of camelCase
in whole code.
@ishakalra: The indentation is good now. The issue is due to non zero return code by the |
The build is now fixed 🎉. Please take a look at comments from @mohitkyadav and @Monal5031 and make if needed. |
Used underscores in place of camelCases, added spaces and blank lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ishakalra Some minor changes left in code indentations, code looks good otherwise 👍
using namespace std; | ||
|
||
int binary_search_iterative(int array[], int start, int end, int find) | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indentation is not according to standard cpp guidelines.
The opening curly brace always starts in same line.
int name_of_func(...) {
Same changes in rest of the file.
|
||
int binary_search_iterative(int array[], int start, int end, int find) | ||
{ | ||
while(end >= start) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make this, while (...) {
as per standard cpp guidelines. Same changes in rest of the file.
{ | ||
int mid = start + (end - start)/2; | ||
if(array[mid] == find) | ||
return mid; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add whitespace around binary operator /
. Same changes in rest of the file.
int mid = start + (end - start)/2; | ||
if(array[mid] == find) | ||
return mid; | ||
else if(array[mid] < find) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else if (...) {
Same changes in rest of the file.
while(end >= start) | ||
{ | ||
int mid = start + (end - start)/2; | ||
if(array[mid] == find) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (...) {
Same changes in rest of the file.
Fixes #548
By submitting this pull request I confirm I've read and complied with the below declarations.
Added {Algorithm/DS name} [{Language}]
, notUpdate README.md
orAdded new code
.