Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign up# Added BinarySearch algorithm #1389
Conversation
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) { |
rbshealy
Aug 5, 2020
Contributor
use public static int binarySearch(...)
use public static int binarySearch(...)
int mid = (right-left)/2; | ||
if(left<right) |
rbshealy
Aug 5, 2020
Contributor
Add whitespace to make your code more readable
int mid = (right-left)/2;
if(left<right)
Add whitespace to make your code more readable
int mid = (right-left)/2;
if(left<right)
if(left<right) | ||
{ | ||
if(a[mid] ==x) |
rbshealy
Aug 5, 2020
Contributor
reformat bracketing to one line
if(left < right) {
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; |
rbshealy
Aug 5, 2020
Contributor
Please use indentation and brackets, and clean up these if else-if statements
This is hard to read
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++) { |
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++) {
add white space between declaration/assignment and logic
i.e.
int[] a = new int[n];
for (int i = 0; i < n; i++) {
Huzaib
Aug 5, 2020
Author
Ok I am on it
Ok I am on it
Sent from Yahoo Mail for iPhone
On Thursday, August 6, 2020, 3:36 am, Huzaib <[email protected]> wrote:
@Huzaib commented on this pull request.
In divideconquer/BinarySearch.java:
+ int[] a = new int[n];
+ for (int i = 0; i < n; i++) {
Ok I am on it
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Sir, will you look at it now? |
Oddly enough I can't see the changes you've made |
Sorry sir, it was my mistake |
Resolve all issues and then Ill take another look |
I think everything is done |
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) { | ||
|
afadhitya
Aug 9, 2020
Please clear unnecessary whitespace
Please clear unnecessary whitespace
|
||
public class BinarySearch { | ||
|
||
public static int binarySearch(int[] a, int x, int left, int right) { |
afadhitya
Aug 9, 2020
I think it will be better if you add javadoc too
I think it will be better if you add javadoc too
} | ||
} | ||
|
||
static class FastScanner { |
afadhitya
Aug 9, 2020
Please add the access modifier
Please add the access modifier
afadhitya
Aug 9, 2020
Also in other field and method
Also in other field and method
Searches for an element inside a sorted array in O(logn) time complexity