Skip to content
Related Articles

Related Articles

Improve Article

Binary Search on Java Vector

  • Last Updated : 26 Oct, 2020

Vector is a legacy class in Java and is present from Java 1.2 version. It implements the List interface of the Collection framework and is found in java.util package. Vector is just like an array that can grow dynamically. Vectors are synchronized ie vectors are thread-safe. Vectors are mainly used where thread synchronization is of utmost importance. In other cases, ArrayList performs better. Since vector is a legacy class in Java, the elements in a vector can be iterated over by Enumeration only. In this article, we shall see a binary search operation on a vector. Binary search is a search technique in which a sorted array is repeatedly divided into half and the middle element is checked for the target element. To search a target element in a vector we will be using the binarySearch() method of the Collections class.

Syntax:

binarySearch(List list, Object target)

Parameters: 

  • list: The first parameter is the List type object on which binary search is to be performed.
  • target: The second parameter is the value of the target element.

Return Value: Returns the index of the target element if it is found else returns -1.

Example:



Java




// Java program to demonstrate binary search on vector
  
import java.util.*;
  
class BinarySearchOnVector {
  
    public static void main(String[] args)
    {
        // creating a Vector object
        Vector<Integer> v = new Vector<>();
  
        // Add elements to Vector
        v.add(10);
        v.add(50);
        v.add(20);
        v.add(40);
        v.add(25);
  
        // binary search works only on sorted list
        Collections.sort(v);
  
        // search an element using binarySearch method of
        // Collections class
        int index = Collections.binarySearch(v, 25);
  
        // print the position of target
        System.out.println("Element is found at index : "
                           + index);
    }
}
Output
Element is found at index : 2

Explanation:

The vector elements were not added in the sorted order. Since binary search works only on the sorted list, we sort the vector first and then perform a binary search. The target is found at index 2 and hence the value is returned and printed.

Time Complexity: O(log n) where n is the size of the vector.

Example 2:

Java




// Java program to demonstrate binary search on vector
  
import java.util.*;
  
class BinarySearchVectorExample {
  
    public static void main(String[] args)
    {
        // creating a Vector object
        Vector<String> v = new Vector<>();
  
        // Add elements to Vector
        v.add("10");
        v.add("B");
        v.add("20");
        v.add("A");
        v.add("25");
  
        // binary search works only on sorted list
        Collections.sort(v);
        System.out.println("Sorted Vector: " + v);
  
        // search an element using binarySearch method of
        // Collections class
        int index = Collections.binarySearch(v, "25");
  
        // prin the position of target
        System.out.println("Element is found at index : "
                           + index);
    }
}
Output
Sorted Vector: [10, 20, 25, A, B]
Element is found at index : 2

Explanation: The numeric strings are positioned before the alphabetic strings. Now the target string 25 lies at index 2 and hence the value is returned and printed.

Time Complexity: O(log n) where n is the size of the vector.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :