FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Algorithm/Algorithm_full_features/BinarySearch.java at master · shellteo/Algorithm · GitHub
shellteo
/
Algorithm
Public
forked from
OneCodeMonkey/Algorithm
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Algorithm
/
Algorithm_full_features
/
BinarySearch.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
73 lines (67 loc) · 1.91 KB
Breadcrumbs
Algorithm
/
Algorithm_full_features
/
BinarySearch.java
Copy path
File metadata and controls
73 lines (67 loc) · 1.91 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
java
.
util
.
Arrays
;
/**
* The class provides a static method for binary searching for an integer in a sorted array of integers.
* The `indexOf` operations takes logarithmic time in the worst case.
*
*/
public
class
BinarySearch
{
private
BinarySearch
{}
/**
* Returns the index of the specified key in the specified array.
*
* @param a: the array if integers, must be sorted in ascending order
* @param key: the search key
* @return the index of key in array if present, -1 otherwise
*
*/
public
static
int
indexOf
(
int
[]
a
,
int
key
) {
int
lo
=
0
;
int
hi
=
a
.
length
-
1
;
while
(
lo
<=
hi
) {
// key is in a[lo, hi] or not exist
int
mid
= (
hi
-
lo
) /
2
+
lo
;
if
(
key
<
a
[
mid
])
hi
=
mid
-
1
;
else
if
(
key
>
a
[
mid
])
lo
=
mid
+
1
;
else
return
mid
;
}
return
-
1
;
}
/**
* Return the index of the specified key in the specified array.
* This function is poorly named because it doesn't give the rank
* if the array has duplicate keys or if the key isn't in the array.
*
* @param key: the search key
* @param a: the array of integers, must be sorted in ascending order
* @return index of key in array if exist, -1 otherwise
*
*/
@
Deprecated
public
static
int
rank
(
int
key
,
int
[]
a
) {
return
indexOf
(
a
,
key
);
}
/**
* Reads in a sequence of integers from the whitelist file, specific as
* a command-line argument; reads in integers from standard input;
* prints to standard output those integers that don't appear in the file.
*
* @param args: the command-line arguments
*
*/
public
static
void
main
(
String
[]
args
) {
// read the integers from file
In
in
=
new
In
(
args
[
0
])
int
[]
whitelist
=
in
.
readAllInts
();
// sort
Arrays
.
sort
(
whitelist
);
// read integer key from standard input, print if not in whitelist
while
(!
StdIn
.
isEmpty
()) {
int
key
=
StdIn
.
readInt
();
if
(
BinarySearch
.
indexOf
(
whitelist
,
key
) == -
1
)
StdOut
.
println
(
key
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL