You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.4 KiB
Java

5 years ago
/*************************************************************************
* Compilation: javac StaticSetOfInts.java
*
* Data type to store a set of integers.
*
*************************************************************************/
import java.util.Arrays;
/**
* The StaticSETofInts class represents a set of integers.
* It supports searching for a given integer is in the set. It accomplishes
* this by keeping the set of integers in a sorted array and using
* binary search to find the given integer.
*
* The rank and contains operations take
* logarithmic time in the worst case.
*
* For additional documentation, see Section 1.2</a> of
* Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class StaticSETofInts {
private int[] a;
/**
* Initializes a set of integers specified by the integer array.
* @param keys the array of integers
* @throws IllegalArgumentException if the array contains duplicate integers
*/
public StaticSETofInts(int[] keys) {
// defensive copy
a = new int[keys.length];
for (int i = 0; i < keys.length; i++)
a[i] = keys[i];
// sort the integers
Arrays.sort(a);
// check for duplicates
for (int i = 1; i < a.length; i++)
if (a[i] == a[i-1])
throw new IllegalArgumentException("Argument arrays contains duplicate keys.");
}
/**
* Is the key in this set of integers?
* @param key the search key
* @return true if the set of integers contains the key; false otherwise
*/
public boolean contains(int key) {
return rank(key) != -1;
}
/**
* Returns either the index of the search key in the sorted array
* (if the key is in the set) or -1 (if the key is not in the set).
* @param key the search key
* @return the number of keys in this set less than the key (if the key is in the set)
* or -1 (if the key is not in the set).
*/
public int rank(int key) {
int lo = 0;
int hi = a.length - 1;
while (lo <= hi) {
// Key is in a[lo..hi] or not present.
int mid = lo + (hi - lo) / 2;
if (key < a[mid]) hi = mid - 1;
else if (key > a[mid]) lo = mid + 1;
else return mid;
}
return -1;
}
}