// package chapter5; /** * Title: Chapter 5, "Arrays" * Description: Examples for Chapter 2 * Copyright: Copyright (c) 2000 * Company: Armstrong Atlantic State University * @author Y. Daniel Liang * @version 1.0 */ // BinarySearch.java: Search a key in a sorted list public class BinarySearch { /**Main method*/ public static void main(String[] args) { int[] list = new int[10]; // Create a sorted list and display it System.out.print("The list is "); for (int i=0; i high) // The list has been exhausted without a match return -1; int mid = (low + high)/2; if (key < list[mid]) return binarySearch(key, list, low, mid-1); else if (key == list[mid]) return mid; else return binarySearch(key, list, mid+1, high); } }