Details of Arrays.binarySearch() method in Java

Posted by bltesar on Sat, 28 Mar 2020 15:23:03 +0100

1. role

Find the specified element in the ordered array by dichotomy, and return the subscript of the element

2. Operation interface

The prototype of the method is: public static int binarySearch(Object[] a, Object key). The function needs to receive two parameters: array name and the elements we need to find

3. return value

The type of return value of this method is integer. The specific return value can be divided into the following two situations:

  • If the element exists in the array, the subscript of the element in the array will be returned

    • For example:

      import java.util.Arrays;
      public class binarySearch {
          public static void main(String[] args) {
              int[] scores = {1, 20, 30, 40, 50};
              //Find element 20 in array scores
              int res = Arrays.binarySearch(scores, 20);
              //Print return results
              System.out.println("res = " + res);
          }
      }
      
      

      Run result: res = 2

  • If the element does not exist in the array, it returns - (insertion point + 1),

    • The insertion point here specifically refers to: if the element exists in the array, the subscript of that element in the array

    • For example:

      import java.util.Arrays;
      public class binarySearch {
          public static void main(String[] args) {
              int[] scores = {1, 20, 30, 40, 50};
              //1. Find the element in the array scores 25
              //You can see from the array that 25 is between 20 and 30,
              //Since the subscript of 20 is 1, the subscript of 25 is 2,
              //The last return value is: - (2 + 1) = - 3
              int res1 = Arrays.binarySearch(scores, 25);
              //2. Similarly, find - 2 in the array
              //You can see that - 2 is smaller than any element in the array
              //So it should be in the first array, so the subscript of - 2 should be 0
              //The last return value is: - (0 + 1) = - 1
              int res2 = Arrays.binarySearch(scores, -2);
              //3. For example, find 55 in the array
              //Because 55 is bigger than any element in the array
              //So it should be at the end of the array, and its subscript is 5
              //The last return value is: - (5 + 1) = - 6
              int res3 = Arrays.binarySearch(scores, 55);
              //Print return results
              System.out.println("res1 = " + res1);
              System.out.println("res1 = " + res2);
              System.out.println("res1 = " + res3);
          }
      }
      

      Operation result:
      res1 = -3
      res1 = -1
      res1 = -6

4. Specific implementation principle

import java.util.Arrays;
public class binarySearch {
    public static void main(String[] args) {
        int[] scores = {1, 20, 30, 40, 50};
        //Call the binarySearch method provided by java
        int a = Arrays.binarySearch(scores, 30);
        //Call the custom binarySearch method
        int b = myBinarySearch(scores, 30);
        System.out.println("a = " + a);
        System.out.println("b = " + b);
    }
    //Custom binary search function
    public static int myBinarySearch(int [] array, int key) {
		int low = 0;
		int high = array.length - 1;
		int mid = (low + high) / 2;
		while(low <= high) {
			if(key < array[mid]) 
				high = mid - 1;
			else if(key > array[mid]) 
				low = mid + 1;
			else 
				return mid;
		}
		return -(1 + low);
	}
}

Operation result:
a = 2
b = 2


Topics: Java