In computer science, a binary search algorithm (or binary chop) is a technique for locating a particular value in a sorted list. The method makes progressively better guesses, and closes in on the location of the sought value by selecting the middle element in the span (which, because the list is in sorted order, is the median value), comparing its value to the target value, and determining if it is greater than, less than, or equal to the target value. A guessed index whose value turns out to be too high becomes the new upper bound of the span, and if its value is too low that index becomes the new lower bound. Only the sign of the difference is inspected: there is no attempt at an interpolation search based on the size of the difference. Pursuing this strategy iteratively, the method reduces the search span by a factor of two each time, and soon finds the target value or else determines that it is not in the list at all. A binary search is an example of a dichotomic divide and conquer search algorithm. [Source: Wikipedia]

The following code shows how the binary search works. Remember that we have used Comparable Array and Comparable objects. Comparable objects are nothing but the objects which have implemented java.lang.Comparable interface containing a single method compareTo. This mean for these objects you define a way to compare with one another to find out whether one object is small than, equal to or bigger than the other one.

/** * @author Kushal Paudyal * www.sanjaal.com/java * Last Modified On: 2009-06-16 */ package com.kushal.utils; /** * A class that shows how to do a binary search on * any sorted Comparable Array. * */ public class BinarySearch { public static final int ITEM_NOT_FOUND = -1; /** * This method takes an array of comparable objects * and a comparable object to find in the array. * Then it does a Binary Search and returns the index. * Return -1 if the object does not exist in the array. */ public static int binarySearch(Comparable[] comparableArray, Comparable obj) { int lowIndex = 0; int highIndex = comparableArray.length - 1; int midIndex; while (lowIndex <= highIndex) { midIndex = (lowIndex + highIndex) / 2; if (comparableArray[midIndex].compareTo(obj) < 0) lowIndex = midIndex + 1; else if (comparableArray[midIndex].compareTo(obj) > 0) highIndex = midIndex - 1; else return midIndex; } return ITEM_NOT_FOUND; } /** * Testing the Binary Search Functionality */ public static void main(String[] args) { /**Using Binary Search for Numbers * Array should be a sorted one */ Comparable[] numbers = new Integer[] { 2, 3, 4, 5, 7, 10 }; int numberToFind = 3; /**Using Binary Search For Strings of Names * Array should be a sorted one. */ Comparable[] names = { "Anthony", "Brad", "Clooney", "Donald", "Eric" }; String nameToFind = "Eric"; /**Finding the location of numberToFind using Binary Search**/ int numberIndex = binarySearch(numbers, numberToFind); /**Finding the location of nameToFind using Binary Search**/ int nameIndex = binarySearch(names, nameToFind); System.out.println(numberToFind + " is available at index: " + numberIndex); System.out.println(nameToFind + " is available at index: " + nameIndex); } }

==============================

Here is the sample output for this program:

3 is available at index: 1 Eric is available at index: 4