Package dsa
Class BinarySearch
java.lang.Object
dsa.BinarySearch
This library implements binary search.
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
indexOf
(double[] a, double item) Returns the index ofitem
in the arraya
, or -1.static int
indexOf
(int[] a, int item) Returns the index ofitem
in the arraya
, or -1.static <T extends Comparable<T>>
intindexOf
(T[] a, T item) Returns the index ofitem
in the sorted arraya
, or -1.static <T> int
indexOf
(T[] a, T item, Comparator<T> c) Returns the index ofitem
in the sorted arraya
, or -1 (comparisons are made using the comparatorc
).static void
Unit tests the library.
-
Method Details
-
indexOf
Returns the index ofitem
in the sorted arraya
, or -1. Comparisons are made according to natural order.- Type Parameters:
T
- the type of items in a.- Parameters:
a
- sorted array of comparable objects.item
- search item.- Returns:
- the index of
item
in the sorted arraya
, or -1.
-
indexOf
Returns the index ofitem
in the sorted arraya
, or -1 (comparisons are made using the comparatorc
).- Type Parameters:
T
- the type of items in a.- Parameters:
a
- sorted array of comparable objects.item
- search item.c
- the comparator.- Returns:
- the index of
item
in the sorted arraya
, or -1.
-
indexOf
public static int indexOf(int[] a, int item) Returns the index ofitem
in the arraya
, or -1.- Parameters:
a
- array of objects.item
- search item.- Returns:
- the index of
item
in the arraya
, or -1.
-
indexOf
public static int indexOf(double[] a, double item) Returns the index ofitem
in the arraya
, or -1.- Parameters:
a
- array of objects.item
- search item.- Returns:
- the index of
item
in the arraya
, or -1.
-
main
Unit tests the library.- Parameters:
args
- the command-line arguments.
-