Here you can find the source of binarySearch(Object[] a, Object key)
public static int binarySearch(Object[] a, Object key)
//package com.java2s; //License from project: Open Source License public class Main { public static int binarySearch(Object[] a, Object key) /* */{/*w w w. ja v a 2 s.c om*/ /* 3669 */int x1 = 0; /* 3670 */int x2 = a.length; /* 3671 */int i = x2 / 2; /* 3672 */while (x1 < x2) { /* 3673 */int c = ((Comparable) a[i]).compareTo(key); /* 3674 */if (c == 0) { /* 3675 */return i; /* */} /* 3677 */if (c < 0) { /* 3678 */x1 = i + 1; /* */} /* */else { /* 3681 */x2 = i; /* */} /* 3683 */i = x1 + (x2 - x1) / 2; /* */} /* 3685 */return -1 * i; /* */} public static int binarySearch(int[] a, int key) /* */{ /* 3697 */return binarySearch(a, key, 0, a.length); /* */} public static int binarySearch(int[] a, int key, int start, int end) /* */{ /* 3711 */int x1 = start; /* 3712 */int x2 = end; /* 3713 */int i = x2 / 2; /* 3714 */while (x1 < x2) { /* 3715 */if (a[i] == key) { /* 3716 */return i; /* */} /* 3718 */if (a[i] < key) { /* 3719 */x1 = i + 1; /* */} /* */else { /* 3722 */x2 = i; /* */} /* 3724 */i = x1 + (x2 - x1) / 2; /* */} /* 3726 */return -1 * i; /* */} }