CodeHub Menu
Home Exercise Practice About Download Portfolio Play Games NEW Privacy Policy Terms Contact
Login / Register

Binary Search in Java

JAVA November 19, 2025 4 views 0 likes
Define a class to perform binary search on a list of integers given below, to search for an element input by the user, if it is found display the element along with its position, otherwise display the message "Search element not found".

2, 5, 7, 10, 15, 20, 29, 30, 46, 50

Answer:

Binary Search Program in Java

Binary Search Program in Java


import java.util.Scanner;

public class KboatBinarySearch
{
    public static void main(String args[]) {
        
        Scanner in = new Scanner(System.in);
        int arr[] = {2, 5, 7, 10, 15, 20, 29, 30, 46, 50};
        
        System.out.print("Enter number to search: ");
        int n = in.nextInt();
        
        int l = 0, h = arr.length - 1, index = -1;
        while (l <= h) {
            int m = (l + h) / 2;
            if (arr[m] < n)
                l = m + 1;
            else if (arr[m] > n)
                h = m - 1;
            else {
                index = m;
                break;
            }
        }
        
        if (index == -1) {
            System.out.println("Search element not found");
        }
        else {
            System.out.println(n + " found at position " + index);
        }
    }
}

OUTPUT:


Binary Search - JavaScript

Binary Search Program

Array Used: [2, 5, 7, 10, 15, 20, 29, 30, 46, 50]

4 views 0 likes
Total visits: 2,089 • Unique visitors: 1,285