The example aims to perform binary search for an element of short array in Java.
Source Code
package com.beginner.examples;
import java.util.Arrays;
public class BinarySearchShortArray {
public static void main(String[] args) {
//create short array.
short shortArray[] = {1,2,3,4,5};
/*
* Please note that the short array MUST BE SORTED before it can be searched
* using binarySearch method.
*/
//sort byte array using Arrays.sort method
Arrays.sort(shortArray);
short searchValue = 3;
int result = Arrays.binarySearch(shortArray,searchValue);
System.out.println("Result of binary search of 3 is : " + result);
}
}
Output:
Result of binary search of 3 is : 2
References
Imported packages in Java documentation: