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

Binary Search #754

Open
Ansari-Shamaila opened this issue Oct 3, 2024 · 1 comment
Open

Binary Search #754

Ansari-Shamaila opened this issue Oct 3, 2024 · 1 comment

Comments

@Ansari-Shamaila
Copy link

Optimized version of binary search:---->>
import java.util.Scanner;

public class BinarySearch {

// Binary Search function
public static int binarySearch(int[] arr, int x) {
    int start = 0;
    int end = arr.length - 1;

    while (start <= end) {
        int mid = start + (end - start) / 2;  // To prevent overflow

        if (arr[mid] == x) {
            return mid;  // Element found, return index
        } else if (arr[mid] < x) {
            start = mid + 1;  // Move right
        } else {
            end = mid - 1;  // Move left
        }
    }
    return -1;  // Element not found
}

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    // Input array size
    int n = sc.nextInt();
    int[] arr = new int[n];

    // Input array elements
    for (int i = 0; i < n; i++) {
        arr[i] = sc.nextInt();
    }

    // Input number of test cases
    int t = sc.nextInt();

    // For each test case, perform binary search
    for (int i = 0; i < t; i++) {
        int x = sc.nextInt();  // Element to search
        int result = binarySearch(arr, x);
        System.out.println(result);
    }
}

}

@Ulton321
Copy link

Ulton321 commented Jan 2, 2025

This should optimize even more, you can try this:

import java.util.Arrays;
import java.util.Scanner;

public class OptimizedBinarySearch {

    public static int binarySearch(int[] arr, int x) {
        int start = 0;
        int end = arr.length - 1;

        // Early exit if x is outside the array bounds
        if (x < arr[start] || x > arr[end]) {
            return -1;
        }

        while (start <= end) {
            int mid = start + ((end - start) >> 1); // Bitwise shift to calculate mid efficiently

            if (arr[mid] == x) {
                return mid; // Element found, return index
            } else if (arr[mid] < x) {
                start = mid + 1; // Move right
            } else {
                end = mid - 1; // Move left
            }
        }
        return -1; // Element not found
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Input array size and elements
        int n = sc.nextInt();
        int[] arr = new int[n];

        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        // Sort the array to ensure binary search correctness
        Arrays.sort(arr);

        // Input number of test cases
        int t = sc.nextInt();

        // For each test case, perform binary search
        for (int i = 0; i < t; i++) {
            int x = sc.nextInt(); // Element to search
            int result = binarySearch(arr, x);
            System.out.println(result);
        }
    }
}


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

No branches or pull requests

2 participants