14. Binary Search
Binary search to find an item or an insertion point.
-
Binary search: the traditional function returning an index where the wanted item is found, or
-1
if the item is not found.public static int binarySearch(ArrayList<String> data, String wanted);
-
Binary insert: this function finds the index (
idx
) at which a new item fits in the ordering, so thatdata.add(idx, newItem)
results indata
still being sorted.public static int binaryInsert(ArrayList<String> data, String newItem);