import java.util.List; public class Searching { public static boolean linearSearch( List list, int value) { for (int data : list) { if (value == data) { return true; } } return false; } /* pre: list is sorted */ public static boolean binarySearch(List list, int value) { return binarySearch(list, value, 0, list.size()); } /* Returns true if the sublist of list between lo (inclusive) and hi (exclusive) contains value */ private static boolean binarySearch(List list, int value, int lo, int hi) { if (lo == hi) { return false; } if (lo == hi - 1) { return list.get(lo) == value; } int size = hi - lo; int mid = size/2 + lo; if (value < list.get(mid)) { return binarySearch(list, value, lo, mid); } else { return binarySearch(list, value, mid, hi); } } }