public class BinarySearch {

    private static int binarySearch(int[] arr, int searchElement) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (arr[mid] == searchElement) { // Element found
                return mid;
            }
            if (arr[mid] < searchElement) { // Look in right half
                left = mid + 1;
            } else { // Look in left half
                right = mid - 1;
            }
        }

        return -1;        // Element not found
    }

    public static void main(String[] args) {
        int[] arr = new int[] {1, 5, 35, 112, 258, 324};
        int[] searchArr = new int[] {1, 35, 112, 324, 67};
        int pos;
        for (int i = 0; i < searchArr.length; i++) {
            pos = binarySearch(arr, searchArr[i]);  //search key and get poistion
            if (pos >= 0) {
                System.out.println(searchArr[i] + "-> found at index : " + pos);
            } else {
                System.out.println(searchArr[i] + "-> not found");
            }
        }
    }
}

@@ -30,15 +30,719 @@
     
-private
+/**%0A     * %3Cp%3ESearches searchElement in arr and returns the index where it's found or -1 if not found.%3C/p%3E%0A     *%0A     * %3Cp%3EThe given array must be already sorted otherwise the results are undefined.%3C/p%3E%0A     *%0A     * %3Cp%3EIf there a duplicate elements, there is no guarantee which one will be found.%3C/p%3E%0A     *%0A     * %3Cp%3EThis implementation avoids a possible overflow while calculating the index for the middle element.%0A     * @see %3Ca href=%22https://research.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html%22%3EExtra, Extra -%0A     * Read All About It: Nearly All Binary Searches and Mergesorts are Broken%3C/a%3E%3C/p%3E%0A     *%0A     * @param arr%0A     * @param searchElement%0A     * @return%0A     */%0A    public
  sta
@@ -910,27 +910,89 @@
 d = 
-(
 left + 
+(
 right
-) / 2;
+ - left) / 2; // identical to (left + right) / 2 but avoids overflow
 %0A   

Fix #543: Add documentation and avoid overflow operation for Binary Search [Java] (#561)