Binary Search in Java

Binary Search in Java

What is Binary Search?

Binary Search Algorithm is a perfect example of divide and conquer technique, as in binary search in java, we divide the array in 2 parts and search for the element in either of the part accordingly. 

Here, in the page we will discuss more about the Binary Search in java.

Searching in Java:

Searching is a Process of finding an element with it’s Location in the program. There are mainly two types of Searching techniques that are being used in java. These are:

  1. Linear Search
  2. Binary Search

Searching is being used in the program in accordance of time and space complexity of the searching algorithms.

Difference Between Linear And Binary Search In Java:

Linear Search Algorithm works linearly in the program which mean Every element of the given array is being checked with the searching element.
Binary Search Algorithm works on the sorted array and divides the given array of elements in two parts and Searches the element accordingly. The average time and space complexity of binary search is better as compared to linear search algorithm.

Binary Search in Java:

Following are the steps to be followed for the binary search algorithm:

  • Start your search using the middle element of the entire array.
  • If the value of the element that is to be searched equals to the mid element then return the index of the mid element.
  • Alternately, if the value is smaller than the element that is to be searched in the interval’s midpoint, limit the interval to its lower half.
  • If the searching element is bigger, restrict it to the upper half.
  • Repeat the process again until the value is discovered or the array is empty.
binary search in java

Note: we can directly use binarysearch() function of collection framework in java rather than implementing the whole algorithm in the program

Example:

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        int[] arr = {3,2,1,7,5,6,4};

        // Sorting the Given Array
        Arrays.sort(arr);    
        System.out.print("The Given Array is : ");
        
        // Printing the array using loops
        for(int i = 0;i<arr.length;i++){
            System.out.print(arr[i] + " ");
        }
        System.out.println();

        int len = Arrays.binarySearch(arr,3);    // using binarysearch function
        System.out.println("Element is Found at " + (len+1) + " Position." );
    }
}
Output:

The Given Array is : 1 2 3 4 5 6 7 
Element is Found at 3 Position.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription