Skip to content
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

Closed
wants to merge 4 commits into from
Closed

Added Binary Search [C++] #550

wants to merge 4 commits into from

Conversation

ishakalra
Copy link

@ishakalra ishakalra commented Oct 19, 2017

Fixes #548

By submitting this pull request I confirm I've read and complied with the below declarations.

  • I have read the Contribution guidelines and I am confident that my PR reflects them.
  • I have followed the coding guidelines for this project.
  • My code follows the skeleton code structure.
  • This pull request has a descriptive title. For example, Added {Algorithm/DS name} [{Language}], not Update README.md or Added new code.
  • This pull request will be closed if I fail to update it even once in a continuous time span of 7 days.

@singhpratyush
Copy link
Member

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!

@singhpratyush
Copy link
Member

@ishakalra: Please fix travis build. Thanks :)

@ishakalra
Copy link
Author

@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!

Copy link
Member

@mohitkyadav mohitkyadav left a 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

@@ -0,0 +1,38 @@
#include<iostream>
using namespace std;
Copy link
Member

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.

#include<iostream>
using namespace std;
int binarySearchIterative(int array[], int start, int end, int find)
{
Copy link
Member

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

{
while(end >= start)
{
int mid = start + (end - start)/2;
Copy link
Member

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.

}
return -1;
}
int binarySearchRecursive(int array[], int start, int end, int find)
Copy link
Member

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.

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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put space around <<.

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;
Copy link
Member

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.

Copy link
Member

@Monal5031 Monal5031 left a 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.

@@ -0,0 +1,38 @@
#include<iostream>
using namespace std;
int binarySearchIterative(int array[], int start, int end, int find)
Copy link
Member

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.

@singhpratyush
Copy link
Member

@ishakalra: The indentation is good now. The issue is due to non zero return code by the main method. Travis build would be fixed after making it return 0.

main method now returns 0.
@singhpratyush
Copy link
Member

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.
Copy link
Member

@Monal5031 Monal5031 left a 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)
{
Copy link
Member

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)
Copy link
Member

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;
Copy link
Member

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)
Copy link
Member

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)
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Binary Search [C++]
4 participants