Search element with binary search - Java Program

Q. Write a Java code to search an element from the array using binary search technique.

Answer:

Binary Search:
Search a sorted array by dividing the search interval in half.  If the value of the search key is less than the item in the middle of the interval, narrow the interval to another half. Otherwise narrow it to the upper half.

In this example, we perform the binary search to find an element in the given array.

Binary_Search.java

import java.util.Scanner;
class Binary_Search
{
        public static void main(String[] args)
        {
                int arr[] = {2,4,5,7,6,9,12,15,18};
                int first, mid, last;
                System.out.println("Given array element:");
                for (int i=0; i<arr.length ;i++ )
                {
                        System.out.print(arr[i]+" ");
                }
                System.out.println("\n====================");
                Scanner scn = new Scanner(System.in);
                System.out.print("\nEnter the element to search:");
                int search = scn.nextInt();
                first = 0;
                last = arr.length-1;
                mid = (first+last)/2;
                while (first <= last)
                {
                        if (arr[mid] < search)
                        {
                                first = mid + 1;
                        }
                        else if (arr[mid] == search)
                        {
                                System.out.println("\n"+search+" is found at location: "+ (mid+1));
                                break;
                        }
                        else
                        {
                                last = mid - 1;
                        }
                        mid = (first + last) /2;
                }
                if (first > last)
                {
                        System.out.println("Element is not found");
                }
        }
}


Output:

binary search