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

BinarySearch #27

Closed
wants to merge 2 commits into from
Closed

BinarySearch #27

wants to merge 2 commits into from

Conversation

Karthikeya914
Copy link
Contributor

@Karthikeya914 Karthikeya914 commented Oct 29, 2024

Summary by Sourcery

New Features:

  • Implement a binary search algorithm in Java that reads input from the user, sorts the array, and performs search queries.

Copy link
Contributor

sourcery-ai bot commented Oct 29, 2024

Reviewer's Guide by Sourcery

This 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 implementation

classDiagram
    class BinarySearch {
        +main(String[] args)
    }
Loading

File-Level Changes

Change Details Files
Implementation of Binary Search algorithm with user input handling
  • Creates a Scanner object to handle user input for array size, number of queries, and array elements
  • Implements array initialization and sorting before search operations
  • Implements binary search logic using start, end, and mid pointers
  • Handles multiple search queries in a loop
  • Provides output indicating whether element was found and its position
Java/BinarySearch.java

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a 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

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +18 to +29
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;
}
}
Copy link
Contributor

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 {
Copy link
Contributor

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:

  1. Extracts binary search into a reusable method
  2. Separates input array handling
  3. Simplifies the main loop by using the search result directly
  4. Reduces nesting depth and removes the need for the isfound flag

@x0lg0n
Copy link
Owner

x0lg0n commented Oct 29, 2024

Resolve the conflicts first then I will approve this PR.

@Karthikeya914
Copy link
Contributor Author

Resolve the conflicts first then I will approve this PR.

is it ok now sir ?

@x0lg0n x0lg0n closed this Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants