Open In App

Java Program to Search ArrayList Element Using Binary Search

Last Updated : 16 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Linear Search can be implemented for sorting and non-sorting elements of a Data structure particular Data structure but the average case time complexity is O(n). Whereas as Binary Search can be implemented only when the items are in sorted order and average-case time complexity is O(logn) and both Transversal have best-case Time complexity is O(1). Now, given an Array List containing sorted elements Check whether the element exists in the ArrayList or not. 

There are two types of Transversal while searching elements in Linear Data structure.

  1.  Linear Search
  2.  Binary Search.

Illustration:

Input:

ArrayList:[1, 2, 3, 4, 6, 7, 8, 9]
key:3

Output:
true

Case 1: Use Binary Search Because the list is sorted in order and Binary Search has less average time complexity as compared to Linear Search i.e O(logn).

Java




// Java Program to Search ArrayList Element
// Using Binary Search
 
// Importing generic java libraries
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Method to search elements in ArrayList
    static boolean search(int key, ArrayList<Integer> A)
    {
        // low pointer
        int low = 0;
 
        // high pointer
        int high = A.size() - 1;
 
        while (low <= high) {
 
            // find the mid pointer
            int mid = low + (high - low) / 2;
            if (A.get(mid) == key) {
                return true;
            }
            else if (A.get(mid) < key) {
 
                // shift the low pointer
                low = mid + 1;
            }
            else {
 
                // shift the high pointer
                high = mid - 1;
            }
        }
        return false;
    }
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an ArrayList
        ArrayList<Integer> A = new ArrayList<>();
 
        // Adding items in the list
        A.add(1);
        A.add(2);
        A.add(3);
        A.add(4);
        A.add(6);
        A.add(7);
        A.add(8);
        A.add(9);
 
        // Random element to be searched
        int key = 19;
 
        // Binary search
        boolean check = search(key, A);
 
        System.out.println(check);
 
        int key1 = 2;
 
        // Binary search
        boolean check1 = search(key1, A);
 
        System.out.println(check1);
    }
}


Output:

false
true

Case 2: Suppose in order to find the maximum index of the greatest element less than the key in sorted repeated elements of ArrayList Using Binary Search.

Input:

List: [2, 3, 3, 4, 4, 5, 6, 7]
key: 2
key: 4

Output:
-1
2

Example: Modify the Binary Search according to the condition

Java




// Java Program to Search ArrayList Element
// Using Binary Search
 
// Importing generic java libraries
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Method to search elements in arrayList
    static int search(int key, ArrayList<Integer> A)
    {
        // Low pointer
        int low = 0;
 
        // High pointer
        int high = A.size() - 1;
        int index = -1;
        while (low <= high) {
 
            // Mid pointer
            int mid = low + (high - low) / 2;
            if (A.get(mid) == key) {
 
                // Shift the High Pointer
                high = mid - 1;
            }
            else if (A.get(mid) < key) {
 
                // Storing the index of mid index value
                index = mid;
 
                // Shift the Low Pointer
                low = mid + 1;
            }
 
            else {
                high = mid - 1;
            }
        }
 
        // Return the index
        return index;
    }
 
    public static void main(String[] args)
    {
        // Creating an ArrayList
        ArrayList<Integer> A = new ArrayList<>();
 
        // Adding elements in ArrayList
        A.add(2);
        A.add(3);
        A.add(3);
        A.add(4);
        A.add(4);
        A.add(5);
        A.add(6);
        A.add(7);
 
        // Key
        int key = 5;
 
        // Index of smallest greater element
        int index = search(key, A);
 
        // Print searched element
        System.out.println(index);
    }
}


Output:

4

Time complexity: O(logn)

Auxiliary space: O(1) as it is using constant variables



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads