Class GenericBinarySearch


  • public class GenericBinarySearch
    extends Object
    Generic binary search that can accepts lists of Java objects that can be adapted to a double value for searching.
    Author:
    bdferris
    • Constructor Detail

      • GenericBinarySearch

        public GenericBinarySearch()
    • Method Detail

      • search

        public static <T> int search​(List<T> elements,
                                     double targetValue,
                                     GenericBinarySearch.ValueAdapter<T> valueAdapter)
        Return an index into the element list such that if a new element with the specified target value was inserted into the list at the specified index, the list would remain in sorted order with respect to the GenericBinarySearch.ValueAdapter
        Parameters:
        elements - a list of objects, sorted in the order appropriate to the GenericBinarySearch.ValueAdapter
        targetValue - target value to search for
        valueAdapter - adapter to convert the input element type into a double value
        Returns:
      • search

        public static <T> int search​(T elements,
                                     int size,
                                     double targetValue,
                                     GenericBinarySearch.IndexAdapter<T> valueAdapter)
        Return an index into the element list such that if a new element with the specified target value was inserted into the list at the specified index, the list would remain in sorted order with respect to the GenericBinarySearch.ValueAdapter
        Parameters:
        elements - a list of objects, sorted in the order appropriate to the GenericBinarySearch.ValueAdapter
        targetValue - target value to search for
        valueAdapter - adapter to convert the input element type into a double value
        Returns:
      • searchRange

        public static <T> int searchRange​(T elements,
                                          int indexFrom,
                                          int indexTo,
                                          double targetValue,
                                          GenericBinarySearch.IndexAdapter<T> valueAdapter)
        Return an index into the element list such that if a new element with the specified target value was inserted into the list at the specified index, the list would remain in sorted order with respect to the GenericBinarySearch.IndexAdapter
        Parameters:
        elements - a collection of objects, sorted in the order appropriate to the GenericBinarySearch.IndexAdapter
        indexFrom - starting index range
        indexTo - ending index range
        targetValue - target value to search for
        valueAdapter - adapter to convert the input element type into a double value
        Returns: