Package dsa

Class BinarySearch

java.lang.Object
dsa.BinarySearch

public class BinarySearch extends Object
This library implements binary search.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    indexOf(double[] a, double item)
    Returns the index of item in the array a, or -1.
    static int
    indexOf(int[] a, int item)
    Returns the index of item in the array a, or -1.
    static <T extends Comparable<T>>
    int
    indexOf(T[] a, T item)
    Returns the index of item in the sorted array a, or -1.
    static <T> int
    indexOf(T[] a, T item, Comparator<T> c)
    Returns the index of item in the sorted array a, or -1 (comparisons are made using the comparator c).
    static void
    main(String[] args)
    Unit tests the library.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • indexOf

      public static <T extends Comparable<T>> int indexOf(T[] a, T item)
      Returns the index of item in the sorted array a, 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 array a, or -1.
    • indexOf

      public static <T> int indexOf(T[] a, T item, Comparator<T> c)
      Returns the index of item in the sorted array a, or -1 (comparisons are made using the comparator c).
      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 array a, or -1.
    • indexOf

      public static int indexOf(int[] a, int item)
      Returns the index of item in the array a, or -1.
      Parameters:
      a - array of objects.
      item - search item.
      Returns:
      the index of item in the array a, or -1.
    • indexOf

      public static int indexOf(double[] a, double item)
      Returns the index of item in the array a, or -1.
      Parameters:
      a - array of objects.
      item - search item.
      Returns:
      the index of item in the array a, or -1.
    • main

      public static void main(String[] args)
      Unit tests the library.
      Parameters:
      args - the command-line arguments.