-
Notifications
You must be signed in to change notification settings - Fork 41
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
BinarySearch #27
BinarySearch #27
Conversation
Reviewer's Guide by SourceryThis PR implements a Binary Search algorithm in Java. The implementation includes a main method that reads input from the user, performs binary search on a sorted array, and outputs whether the searched element was found along with its position. Class diagram for BinarySearch implementationclassDiagram
class BinarySearch {
+main(String[] args)
}
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
Hey @Karthikeya914 - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider extracting the binary search logic into a separate method that returns the found index (or -1 if not found). This would improve reusability and testability of the code.
- Use more descriptive variable names - 'q' could be 'numQueries', 'x' could be 'searchTarget', etc. This makes the code more readable and self-documenting.
Here's what I looked at during the review
- 🟡 General issues: 1 issue found
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟡 Complexity: 1 issue found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
while(start <= end) { | ||
int mid = start + (end - start) / 2; | ||
if (arr[mid] > x) { | ||
end = mid - 1; | ||
} else if (arr[mid] < x) { | ||
start = mid + 1; | ||
} else { | ||
System.out.println("found " + mid); | ||
isfound = true; | ||
break; | ||
} | ||
} |
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.
suggestion: Consider extracting binary search logic into a separate method
Moving the binary search logic into a dedicated method would improve code modularity and reusability, separating the algorithm from I/O operations.
private static int binarySearch(int[] arr, int x) {
int start = 0;
int end = arr.length - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (arr[mid] > x) end = mid - 1;
else if (arr[mid] < x) start = mid + 1;
else return mid;
}
return -1;
}
@@ -0,0 +1,37 @@ | |||
import java.util.*; | |||
|
|||
public class BinarySearch { |
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.
issue (complexity): Consider extracting the binary search logic into a separate method and grouping input handling together.
The code would be more maintainable with the binary search logic extracted into a separate method and input handling grouped together. This reduces nesting and improves readability while maintaining functionality. Here's a suggested refactoring:
public class BinarySearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = readArray(sc);
int q = sc.nextInt();
while (q-- > 0) {
int x = sc.nextInt();
int result = binarySearch(arr, x);
System.out.println(result >= 0 ? "found " + result : "not found");
}
sc.close();
}
private static int[] readArray(Scanner sc) {
int size = sc.nextInt();
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
return arr;
}
private static int binarySearch(int[] arr, int x) {
int start = 0, end = arr.length - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (arr[mid] == x) return mid;
if (arr[mid] > x) end = mid - 1;
else start = mid + 1;
}
return -1;
}
}
This refactoring:
- Extracts binary search into a reusable method
- Separates input array handling
- Simplifies the main loop by using the search result directly
- Reduces nesting depth and removes the need for the
isfound
flag
Resolve the conflicts first then I will approve this PR. |
is it ok now sir ? |
Summary by Sourcery
New Features: