import java.util.List; import java.util.Comparator; public class Search { public static final int NOT_FOUND = -1; public static int linear(List list, Object key) { for (int i = 0; i < list.size(); ++i) if (key == null ? list.get(i) == null : key.equals(list.get(i))) return i; return NOT_FOUND; } private static int binary(List> list, T key, int low, int high) { if (high < low) return NOT_FOUND; var mid = (low + high) / 2; var cmp = list.get(mid).compareTo(key); if (cmp < 0) return binary(list, key, mid + 1, high); if (cmp > 0) return binary(list, key, low, mid - 1); return mid; } public static int binary(List> list, T key) { return binary(list, key, 0, list.size()); } private static int binary(List list, T key, Comparator c, int low, int high) { if (high < low) return NOT_FOUND; var mid = (low + high) / 2; var cmp = c.compare(list.get(mid), key); if (cmp < 0) return binary(list, key, c, mid + 1, high); if (cmp > 0) return binary(list, key, c, low, mid - 1); return mid; } public static int binary(List list, T key, Comparator c) { return binary(list, key, c, 0, list.size()); } }