Package edu.rit.util

Class Searching

java.lang.Object
edu.rit.util.Searching

public class Searching extends Object
Class Searching provides static methods for searching arrays of primitive types and object types.

Note: The operations in class Searching are not multiple thread safe.

Version:
22-Nov-2011
Author:
Alan Kaminsky
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Class Searching.Byte is the base class for a helper object used to search an array of type byte[].
    static class 
    Class Searching.Character is the base class for a helper object used to search an array of type char[].
    static class 
    Class Searching.Double is the base class for a helper object used to search an array of type double[].
    static class 
    Class Searching.Float is the base class for a helper object used to search an array of type float[].
    static class 
    Class Searching.Integer is the base class for a helper object used to search an array of type int[].
    static class 
    Class Searching.Long is the base class for a helper object used to search an array of type long[].
    static class 
    Class Searching.Object is the base class for a helper object used to search an array of type T[].
    static class 
    Class Searching.Short is the base class for a helper object used to search an array of type short[].
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    searchSorted(byte[] x, byte a, Searching.Byte helper)
    Search the given ordered array of type byte[] for the given element.
    static int
    searchSorted(char[] x, char a, Searching.Character helper)
    Search the given ordered array of type char[] for the given element.
    static int
    searchSorted(double[] x, double a, Searching.Double helper)
    Search the given ordered array of type double[] for the given element.
    static int
    searchSorted(float[] x, float a, Searching.Float helper)
    Search the given ordered array of type float[] for the given element.
    static int
    searchSorted(int[] x, int a, Searching.Integer helper)
    Search the given ordered array of type int[] for the given element.
    static int
    searchSorted(long[] x, long a, Searching.Long helper)
    Search the given ordered array of type long[] for the given element.
    static int
    searchSorted(short[] x, short a, Searching.Short helper)
    Search the given ordered array of type short[] for the given element.
    static <T extends Comparable<T>>
    int
    searchSorted(T[] x, T a)
    Search the given ordered array of type T[] for the given element.
    static <T> int
    searchSorted(T[] x, T a, Searching.Object<T> helper)
    Search the given ordered array of type T[] for the given element.
    static <T> int
    searchSorted(T[] x, T a, Comparator<T> comp)
    Search the given ordered array of type T[] for the given element.
    static int
    searchUnsorted(byte[] x, byte a, Searching.Byte helper)
    Search the given unordered array of type byte[] for the given element.
    static int
    searchUnsorted(char[] x, char a, Searching.Character helper)
    Search the given unordered array of type char[] for the given element.
    static int
    searchUnsorted(double[] x, double a, Searching.Double helper)
    Search the given unordered array of type double[] for the given element.
    static int
    searchUnsorted(float[] x, float a, Searching.Float helper)
    Search the given unordered array of type float[] for the given element.
    static int
    searchUnsorted(int[] x, int a, Searching.Integer helper)
    Search the given unordered array of type int[] for the given element.
    static int
    searchUnsorted(long[] x, long a, Searching.Long helper)
    Search the given unordered array of type long[] for the given element.
    static int
    searchUnsorted(short[] x, short a, Searching.Short helper)
    Search the given unordered array of type short[] for the given element.
    static <T extends Comparable<T>>
    int
    searchUnsorted(T[] x, T a)
    Search the given unordered array of type T[] for the given element.
    static <T> int
    searchUnsorted(T[] x, T a, Searching.Object<T> helper)
    Search the given unordered array of type T[] for the given element.
    static <T> int
    searchUnsorted(T[] x, T a, Comparator<T> comp)
    Search the given unordered array of type T[] for the given element.

    Methods inherited from class java.lang.Object

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

    • searchUnsorted

      public static int searchUnsorted(byte[] x, byte a, Searching.Byte helper)
      Search the given unordered array of type byte[] for the given element. The given helper object is used to compare elements for equality only. An O(n) linear search algorithm is used.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      helper - Helper object.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchSorted

      public static int searchSorted(byte[] x, byte a, Searching.Byte helper)
      Search the given ordered array of type byte[] for the given element. The given helper object is used to compare elements for order and equality. It is assumed that the array is sorted in the order determined by the helper object; otherwise, the searchSorted() method's behavior is not specified. An O(log n) binary search algorithm is used.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      helper - Helper object.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchUnsorted

      public static int searchUnsorted(char[] x, char a, Searching.Character helper)
      Search the given unordered array of type char[] for the given element. The given helper object is used to compare elements for equality only. An O(n) linear search algorithm is used.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      helper - Helper object.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchSorted

      public static int searchSorted(char[] x, char a, Searching.Character helper)
      Search the given ordered array of type char[] for the given element. The given helper object is used to compare elements for order and equality. It is assumed that the array is sorted in the order determined by the helper object; otherwise, the searchSorted() method's behavior is not specified. An O(log n) binary search algorithm is used.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      helper - Helper object.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchUnsorted

      public static int searchUnsorted(short[] x, short a, Searching.Short helper)
      Search the given unordered array of type short[] for the given element. The given helper object is used to compare elements for equality only. An O(n) linear search algorithm is used.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      helper - Helper object.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchSorted

      public static int searchSorted(short[] x, short a, Searching.Short helper)
      Search the given ordered array of type short[] for the given element. The given helper object is used to compare elements for order and equality. It is assumed that the array is sorted in the order determined by the helper object; otherwise, the searchSorted() method's behavior is not specified. An O(log n) binary search algorithm is used.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      helper - Helper object.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchUnsorted

      public static int searchUnsorted(int[] x, int a, Searching.Integer helper)
      Search the given unordered array of type int[] for the given element. The given helper object is used to compare elements for equality only. An O(n) linear search algorithm is used.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      helper - Helper object.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchSorted

      public static int searchSorted(int[] x, int a, Searching.Integer helper)
      Search the given ordered array of type int[] for the given element. The given helper object is used to compare elements for order and equality. It is assumed that the array is sorted in the order determined by the helper object; otherwise, the searchSorted() method's behavior is not specified. An O(log n) binary search algorithm is used.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      helper - Helper object.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchUnsorted

      public static int searchUnsorted(long[] x, long a, Searching.Long helper)
      Search the given unordered array of type long[] for the given element. The given helper object is used to compare elements for equality only. An O(n) linear search algorithm is used.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      helper - Helper object.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchSorted

      public static int searchSorted(long[] x, long a, Searching.Long helper)
      Search the given ordered array of type long[] for the given element. The given helper object is used to compare elements for order and equality. It is assumed that the array is sorted in the order determined by the helper object; otherwise, the searchSorted() method's behavior is not specified. An O(log n) binary search algorithm is used.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      helper - Helper object.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchUnsorted

      public static int searchUnsorted(float[] x, float a, Searching.Float helper)
      Search the given unordered array of type float[] for the given element. The given helper object is used to compare elements for equality only. An O(n) linear search algorithm is used.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      helper - Helper object.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchSorted

      public static int searchSorted(float[] x, float a, Searching.Float helper)
      Search the given ordered array of type float[] for the given element. The given helper object is used to compare elements for order and equality. It is assumed that the array is sorted in the order determined by the helper object; otherwise, the searchSorted() method's behavior is not specified. An O(log n) binary search algorithm is used.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      helper - Helper object.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchUnsorted

      public static int searchUnsorted(double[] x, double a, Searching.Double helper)
      Search the given unordered array of type double[] for the given element. The given helper object is used to compare elements for equality only. An O(n) linear search algorithm is used.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      helper - Helper object.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchSorted

      public static int searchSorted(double[] x, double a, Searching.Double helper)
      Search the given ordered array of type double[] for the given element. The given helper object is used to compare elements for order and equality. It is assumed that the array is sorted in the order determined by the helper object; otherwise, the searchSorted() method's behavior is not specified. An O(log n) binary search algorithm is used.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      helper - Helper object.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchUnsorted

      public static <T> int searchUnsorted(T[] x, T a, Searching.Object<T> helper)
      Search the given unordered array of type T[] for the given element. The given helper object is used to compare elements for equality only. An O(n) linear search algorithm is used.
      Type Parameters:
      T - Array element data type.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      helper - Helper object.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchSorted

      public static <T> int searchSorted(T[] x, T a, Searching.Object<T> helper)
      Search the given ordered array of type T[] for the given element. The given helper object is used to compare elements for order and equality. It is assumed that the array is sorted in the order determined by the helper object; otherwise, the searchSorted() method's behavior is not specified. An O(log n) binary search algorithm is used.
      Type Parameters:
      T - Array element data type.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      helper - Helper object.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchUnsorted

      public static <T> int searchUnsorted(T[] x, T a, Comparator<T> comp)
      Search the given unordered array of type T[] for the given element. The given comparator is used to compare elements for equality only. An O(n) linear search algorithm is used.
      Type Parameters:
      T - Array element data type.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      comp - Comparator.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchSorted

      public static <T> int searchSorted(T[] x, T a, Comparator<T> comp)
      Search the given ordered array of type T[] for the given element. The given comparator is used to compare elements for order and equality. It is assumed that the array is sorted in the order determined by the comparator; otherwise, the searchSorted() method's behavior is not specified. An O(log n) binary search algorithm is used.
      Type Parameters:
      T - Array element data type.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      comp - Comparator.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchUnsorted

      public static <T extends Comparable<T>> int searchUnsorted(T[] x, T a)
      Search the given unordered array of type T[] for the given element. The array element's natural ordering (compareTo() method) is used to compare elements for equality only. An O(n) linear search algorithm is used.
      Type Parameters:
      T - Class that extends Comparable.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.
    • searchSorted

      public static <T extends Comparable<T>> int searchSorted(T[] x, T a)
      Search the given ordered array of type T[] for the given element. The array element's natural ordering (compareTo() method) is used to compare elements for order and equality. It is assumed that the array is sorted in the order determined by the natural ordering; otherwise, the searchSorted() method's behavior is not specified. An O(log n) binary search algorithm is used.
      Type Parameters:
      T - Class that extends Comparable.
      Parameters:
      x - Array to be searched.
      a - Element to be searched for.
      Returns:
      If an element the same as a exists in x, then the index of that element is returned. Otherwise, −1 is returned.