Class GenericInequalitySearch


  • public class GenericInequalitySearch
    extends Object
    This provides efficient, unique and unambiguous binary searching for inequalities for ordered arrays of increasing values that may include duplicate values. These inequalities include <, ≤, ==, ≥, >. The same search method can be used for all these inequalities.

    In order to make the searching unique and unambiguous, we modified the traditional binary search algorithm to search for adjacent pairs of values {A, B} in the values array instead of just a single value, where A and B are the array indices of two adjacent values in the array. We then define the searching criteria, given an array of values arr[] and the search key value v.

    Author:
    Lee Rhodes
    • Constructor Detail

      • GenericInequalitySearch

        public GenericInequalitySearch()
    • Method Detail

      • find

        public static <T> int find​(T[] arr,
                                   int low,
                                   int high,
                                   T v,
                                   GenericInequalitySearch.Inequality inequality,
                                   Comparator<T> comparator)
        Binary Search for the index of the generic value in the given search range that satisfies the given inequality. If -1 is returned there are no values in the search range that satisfy the inequality.
        Type Parameters:
        T - The generic type of value to be used in the search process.
        Parameters:
        arr - the given array that must be sorted with increasing values, must not be null, and must not contain null values in the given range {low, high} inclusive.
        low - the lowest index of the lowest value in the search range, inclusive.
        high - the highest index of the highest value in the search range, inclusive.
        v - the value to search for. It must not be null.
        inequality - one of LT, LE, EQ, GE, GT. It must not be null.
        comparator - for the type T. It must not be null.
        Returns:
        the index of the value in the given search range that satisfies the inequality.