在数组上进行Java二进制搜索


在数组上进行Java二进制搜索

import java.util.Arrays;



public class ArrayDemo {

   public static void main(String[] args) {

      int intArr[] = {30,20,5,12,55};

      Arrays.sort(intArr);

      System.out.println("The sorted int array is:");

      for (int number : intArr) {

         System.out.println("Number = " + number);

      }

      int searchVal = 12;

      int retVal = Arrays.binarySearch(intArr,searchVal);

      System.out.println("The index of element 12 is : " + retVal);

   }

}