Jump to content

Recrutăm designeri audio și video. Aplicați aici

Sign in to follow this  

[JAVA] Binary Search

Recommended Posts


Binary Search



What is Binary Search?

In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the array.


Binary search runs in logarithmic time in the worst case, making O(log n) comparisons, where n is the number of elements in the array. Binary search is faster than linear search except for small arrays. However, the array must be sorted first to be able to apply binary search. There are specialized data structures designed for fast searching, such as hash tables, that can be searched more efficiently than binary search. However, binary search can be used to solve a wider range of problems, such as finding the next-smallest or next-largest element in the array relative to the target even if it is absent from the array.


How to do Binary Search?

Given an array A of n elements with values sorted such that A{0} <= A{1} <= A{2} <=  ..... A{n-1} and target value T, the following subroutine uses binary search to find the index of T in A.


  1. Set L to 0 and R to n-1.
  2. If L > R, the search terminates as unsuccessful.
  3. Set m (the position of the middle element) to the floor of L + R / 2  , which is the greatest integer less than or equal to L + R / 2.
  4. If A[m] < T, set L to m+1 and go to step 2.
  5. If A[m] > T, set R to m-1 and go to step 2.
  6. Now A[m] = T, the search is done; return m.



package binarysearch;

 * @author Azain

public class BinarySearch

    Recursive method to do binary search on an array, takes an 
    integer array left index, right index and key to find as 
    arguments, array should be sorted otherwise result will 
    not be accurate
    public static int binarySearch(int arr[], int left, int right, int key) 
        //continue searching for the key until right index is greater then left index
        if (right >= left) 
            //middle index of left and right index
            int mid = left + (right - left) / 2;
            //Base case, if array middle element is equal to key, the index is returned
            if (arr[mid] == key) 
                return mid; 
            //when the middle element is greater than the key, search the left
            if (arr[mid] > key) 
                return binarySearch(arr, left, mid - 1, key); 
            //when the middle element is less than the key, search the right
            return binarySearch(arr, mid + 1, right, key); 
        //returning -1, when the key is not found in the array
        return -1; 
    public static void main(String[] args) 
        int arr[] = {1, 2 , 3 , 4, 5, 6, 7, 8 , 9};
        int key = 7;
        int result = binarySearch(arr, 0, arr.length - 1, key);
        //if key is found
        if (result == -1)
            System.out.println(key + " not found!");
            System.out.println(key + " found at index " + result);








Edited by Azain

Share this post

Link to post
Share on other sites


Share this post

Link to post
Share on other sites
This topic is now closed to further replies.
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.