The Wayback Machine - https://web.archive.org/web/20201103184423/https://github.com/TheAlgorithms/Java/pull/1389
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 BinarySearch algorithm #1389

Open
wants to merge 7 commits into
base: master
from
Open

# Added BinarySearch algorithm #1389

wants to merge 7 commits into from

Conversation

@Huzaib
Copy link

@Huzaib Huzaib commented Aug 5, 2020

Searches for an element inside a sorted array in O(logn) time complexity

👍

Searches for an element inside a sorted array in O(logn) time complexity

public class BinarySearch {

static int binarySearch(int[] a, int x, int left, int right) {

This comment has been minimized.

@rbshealy

rbshealy Aug 5, 2020
Contributor

use public static int binarySearch(...)

int mid = (right-left)/2;
if(left<right)
Comment on lines 7 to 8

This comment has been minimized.

@rbshealy

rbshealy Aug 5, 2020
Contributor

Add whitespace to make your code more readable
int mid = (right-left)/2;

if(left<right)

if(left<right)
{
if(a[mid] ==x)
Comment on lines 8 to 10

This comment has been minimized.

@rbshealy

rbshealy Aug 5, 2020
Contributor

reformat bracketing to one line
if(left < right) {

if(a[mid] ==x)
return mid;
else if(a[mid]>x)
return binarySearch(a, x, left, mid);
else if(a[mid]<x)
return binarySearch(a, x, mid+1, right);
else
return -1;}
else if(left ==right)
{if(a[mid] ==x)
return mid;}
return -1;
Comment on lines 10 to 21

This comment has been minimized.

@rbshealy

rbshealy Aug 5, 2020
Contributor

Please use indentation and brackets, and clean up these if else-if statements
This is hard to read

int[] a = new int[n];
for (int i = 0; i < n; i++) {
Comment on lines 27 to 28

This comment has been minimized.

@rbshealy

rbshealy Aug 5, 2020
Contributor

add white space between declaration/assignment and logic
i.e.
int[] a = new int[n];

for (int i = 0; i < n; i++) {

This comment has been minimized.

@Huzaib

Huzaib Aug 5, 2020
Author

Ok I am on it

@alphagroupsnsw
Copy link

@alphagroupsnsw alphagroupsnsw commented Aug 5, 2020

@Huzaib
Copy link
Author

@Huzaib Huzaib commented Aug 6, 2020

Sir, will you look at it now?

@rbshealy
Copy link
Contributor

@rbshealy rbshealy commented Aug 6, 2020

Sir, will you look at it now?

Oddly enough I can't see the changes you've made

@Huzaib
Copy link
Author

@Huzaib Huzaib commented Aug 7, 2020

Sorry sir, it was my mistake
I had uploaded the same file again
Sorry for the confusion

@rbshealy
Copy link
Contributor

@rbshealy rbshealy commented Aug 7, 2020

Resolve all issues and then Ill take another look

@Huzaib
Copy link
Author

@Huzaib Huzaib commented Aug 7, 2020

I think everything is done
Can you give it a look please?

Copy link

@afadhitya afadhitya left a comment

It seems your code has a lot of unnecessary whitespaces, please delete it, you don't need every whitespace after a single line. Just place it for a necessary code only

if(left<=right) {

if (arr[mid] == u) {

This comment has been minimized.

@afadhitya

afadhitya Aug 9, 2020

Please clear unnecessary whitespace


public class BinarySearch {

public static int binarySearch(int[] a, int x, int left, int right) {

This comment has been minimized.

@afadhitya

afadhitya Aug 9, 2020

I think it will be better if you add javadoc too

}
}

static class FastScanner {

This comment has been minimized.

@afadhitya

afadhitya Aug 9, 2020

Please add the access modifier

This comment has been minimized.

@afadhitya

afadhitya Aug 9, 2020

Also in other field and method

Huzaib added 3 commits Aug 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants
You can’t perform that action at this time.