public class binarySearch{
public static int binarySearch(int [] nums, int target){
int left=0, right=nums.length-1;
while(left<=right){int middle=left+(right-left)/2;//if (nums[middle]==target) return middle;if (nums[middle] > target) right=middle-1;else if(nums[middle] < target) left=middle+1;else return middle;}return -1;}public static void main(String[] args){int[] a={0, 1,2,3,4,5,6,7,8,9};int target=2;int position = binarySearch(a, target);System.out.println(a[position]);for(int i=0; i<=9; i++){System.out.print(binarySearch(a, i)+\" \");}}
}