FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-coding-problems/src/BinarySearch.java at main · jfullstackdev/java-coding-problems · GitHub
jfullstackdev
/
java-coding-problems
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
6
Star
35
Code
Pull requests
0
Discussions
Actions
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Discussions
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
java-coding-problems
/
src
/
BinarySearch.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
42 lines (31 loc) · 985 Bytes
Breadcrumbs
java-coding-problems
/
src
/
BinarySearch.java
Copy path
File metadata and controls
42 lines (31 loc) · 985 Bytes
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
public
class
BinarySearch
{
public
static
int
binarySearch
(
int
arr
[],
int
low
,
int
high
,
int
key
) {
int
mid
= (
low
+
high
) /
2
;
while
(
low
<=
high
) {
if
(
arr
[
mid
] <
key
) {
low
=
mid
+
1
;
}
else
if
(
arr
[
mid
] ==
key
) {
return
mid
;
}
else
{
high
=
mid
-
1
;
}
mid
= (
low
+
high
) /
2
;
}
if
(
low
>
high
) {
return
-
1
;
}
return
-
1
;
}
public
static
void
main
(
String
[]
args
) {
int
intArray
[] = {
7
,
8
,
9
};
int
key
=
8
;
if
(
binarySearch
(
intArray
,
0
,
5
,
key
) != -
1
) {
System
.
out
.
print
(
"searching `"
);
System
.
out
.
print
(
key
);
System
.
out
.
print
(
"` in the array... it's at index "
);
System
.
out
.
println
(
binarySearch
(
intArray
,
0
,
5
,
key
));
}
else
{
System
.
out
.
println
(
"index not found"
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL