public enum InequalitySearch extends Enum<InequalitySearch>
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. For all the search criteria, when the algorithm has narrowed the search down to a single value or adjacent pair of values, the resolve() method provides the final result of the search. If there is no valid value in the array that satisfies the search criterion, the algorithm will return -1 to the caller.
Given a sorted array of values arr[] and a search key value v, the algorithms for the searching criteria are given with each enum criterion.
Enum Constant and Description |
---|
EQ
Given a sorted array of increasing values arr[] and a key value V,
this criterion instructs the binary search algorithm to find the adjacent pair of
values {A,B} such that A ≤ V ≤ B.
|
GE
Given a sorted array of increasing values arr[] and a key value V,
this criterion instructs the binary search algorithm to find the lowest adjacent pair of
values {A,B} such that A < V ≤ B.
|
GT
Given a sorted array of increasing values arr[] and a key value V,
this criterion instructs the binary search algorithm to find the lowest adjacent pair of
values {A,B} such that A ≤ V < B.
|
LE
Given a sorted array of increasing values arr[] and a key value V,
this criterion instructs the binary search algorithm to find the highest adjacent pair of
values {A,B} such that A ≤ V < B.
|
LT
Given a sorted array of increasing values arr[] and a key value v,
this criterion instructs the binary search algorithm to find the highest adjacent pair of
values {A,B} such that A < v ≤ B.
|
Modifier and Type | Method and Description |
---|---|
abstract String |
desc(double[] arr,
int low,
int high,
double v,
int idx)
Optional call that describes the details of the results of the search.
|
abstract String |
desc(float[] arr,
int low,
int high,
float v,
int idx)
Optional call that describes the details of the results of the search.
|
abstract String |
desc(long[] arr,
int low,
int high,
double v,
int idx)
Optional call that describes the details of the results of the search.
|
abstract String |
desc(long[] arr,
int low,
int high,
long v,
int idx)
Optional call that describes the details of the results of the search.
|
static int |
find(double[] arr,
int low,
int high,
double v,
InequalitySearch crit)
Binary Search for the index of the double value in the given search range that satisfies
the given InequalitySearch criterion.
|
static int |
find(float[] arr,
int low,
int high,
float v,
InequalitySearch crit)
Binary Search for the index of the float value in the given search range that satisfies
the given InequalitySearch criterion.
|
static int |
find(long[] arr,
int low,
int high,
double v,
InequalitySearch crit)
Binary Search for the index of the double value in the given search range that satisfies
the given InequalitySearch criterion.
|
static int |
find(long[] arr,
int low,
int high,
long v,
InequalitySearch crit)
Binary Search for the index of the long value in the given search range that satisfies
the given InequalitySearch criterion.
|
static InequalitySearch |
valueOf(String name)
Returns the enum constant of this type with the specified name.
|
static InequalitySearch[] |
values()
Returns an array containing the constants of this enum type, in
the order they are declared.
|
public static final InequalitySearch LT
If v > arr[high], return arr[high].
If v ≤ arr[low], return -1.
Else return index of A.
public static final InequalitySearch LE
If v ≥ arr[high], return arr[high].
If v < arr[low], return -1.
Else return index of A.
public static final InequalitySearch EQ
public static final InequalitySearch GE
If v ≤ arr[low], return arr[low].
If v > arr[high], return -1.
Else return index of B.
public static final InequalitySearch GT
If v < arr[low], return arr[low].
If v ≥ arr[high], return -1.
Else return index of B.
public static InequalitySearch[] values()
for (InequalitySearch c : InequalitySearch.values()) System.out.println(c);
public static InequalitySearch valueOf(String name)
name
- the name of the enum constant to be returned.IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is nullpublic abstract String desc(double[] arr, int low, int high, double v, int idx)
arr
- The underlying sorted array of valueslow
- the low index of the rangehigh
- the high index of the rangev
- the value to search foridx
- the resolved index from the searchpublic abstract String desc(float[] arr, int low, int high, float v, int idx)
arr
- The underlying sorted array of valueslow
- the low index of the rangehigh
- the high index of the rangev
- the value to search foridx
- the resolved index from the searchpublic abstract String desc(long[] arr, int low, int high, long v, int idx)
arr
- The underlying sorted array of valueslow
- the low index of the rangehigh
- the high index of the rangev
- the value to search foridx
- the resolved index from the searchpublic abstract String desc(long[] arr, int low, int high, double v, int idx)
arr
- The underlying sorted array of valueslow
- the low index of the rangehigh
- the high index of the rangev
- the value to search foridx
- the resolved index from the searchpublic static int find(double[] arr, int low, int high, double v, InequalitySearch crit)
arr
- the given array of comparable values that must be sorted with increasing values.
The array must not be null and the values of the array must not be NaN in the range [low, high].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 NaN.crit
- one of the InequalitySearch criteria: LT, LE, EQ, GT, GE. It must not be null.public static int find(float[] arr, int low, int high, float v, InequalitySearch crit)
arr
- the given array that must be sorted.
It must not be null and must not contain any NaN values in the 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 NaN.crit
- one of LT, LE, EQ, GT, GEpublic static int find(long[] arr, int low, int high, long v, InequalitySearch crit)
arr
- the given array that must be sorted.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.crit
- one of LT, LE, EQ, GT, GEpublic static int find(long[] arr, int low, int high, double v, InequalitySearch crit)
arr
- the given array that must be sorted.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.crit
- one of LT, LE, EQ, GT, GECopyright © 2015–2024 The Apache Software Foundation. All rights reserved.