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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Java/BinarySearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,37 @@

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

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int q = sc.nextInt();
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
while (q > 0) {
boolean isfound = false;
int x = sc.nextInt();
int start = 0;
int end = size - 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 {
System.out.println("found " + mid);
isfound = true;
break;
}
}
Comment on lines +18 to +29
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;
}

q--;
if (!isfound) {
System.out.println("not found");
}
}
sc.close();
try (Scanner sc = new Scanner(System.in)) {
int size;
int numQueries;
Expand Down
Loading