StaticSETofInts.java
3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/******************************************************************************
* Compilation: javac StaticSetOfInts.java
* Execution: none
* Dependencies: StdOut.java
*
* Data type to store a set of integers.
*
******************************************************************************/
package edu.princeton.cs.algs4;
import java.util.Arrays;
/**
* The {@code 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.
* <p>
* The <em>rank</em> and <em>contains</em> operations take
* logarithmic time in the worst case.
* <p>
* For additional documentation, see <a href="http://algs4.cs.princeton.edu/12oop">Section 1.2</a> of
* <i>Algorithms, 4th Edition</i> 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;
}
}
/******************************************************************************
* Copyright 2002-2016, Robert Sedgewick and Kevin Wayne.
*
* This file is part of algs4.jar, which accompanies the textbook
*
* Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
* Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
* http://algs4.cs.princeton.edu
*
*
* algs4.jar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* algs4.jar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with algs4.jar. If not, see http://www.gnu.org/licenses.
******************************************************************************/