// 10/27/2017 import java.util.Scanner; /** Binary Search */ public class BinSearch { public static void main(String[] args) { int[] arr = {1, 3, 5, 18, 60, 65, 1000}; int key=65; // number to search for int beg=0, end=arr.length-1; int middle; // while there are still elements to check (the range is not empty) while (beg<=end) { // calculate the index of middle, which is the halfpoint bw beg and end middle = (beg+end)/2; System.out.println("entering while loop, beg=" +beg + " end=" + end + " middle=" + middle); if (key==arr[middle]) { System.out.println("found at location " + middle); beg=end+1; // to make sure that we fall out of loop // can put a boolean found or return } else if (key>arr[middle]) beg=middle+1; else end=middle-1; } // end while loop } // end main } // end class BinSearch