FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-simple-program/binarySearch.java at main · zrylzfra/java-simple-program · GitHub
zrylzfra
/
java-simple-program
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java-simple-program
/
binarySearch.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
49 lines (43 loc) · 1.41 KB
Breadcrumbs
java-simple-program
/
binarySearch.java
Copy path
File metadata and controls
49 lines (43 loc) · 1.41 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
import
java
.
util
.
Arrays
;
import
java
.
util
.
Collections
;
import
java
.
util
.
Scanner
;
public
class
binarySearch
{
static
Scanner
s
=
new
Scanner
(
System
.
in
);
public
static
void
main
(
String
[]
args
) {
System
.
out
.
println
(
"Enter a series of number: "
);
int
num
=
s
.
nextInt
();
s
.
nextLine
();
String
[]
names
=
new
String
[
num
];
System
.
out
.
println
(
"Enter "
+
num
+
" names of Students : "
);
for
(
int
i
=
0
;
i
<
num
;
i
++) {
names
[
i
] =
s
.
nextLine
();
}
System
.
out
.
println
(
"Choices
\n
A. Ascending
\n
B. Decending
\n
Enter your Choice:
\n
"
);
String
ch
=
s
.
nextLine
();
if
(
ch
.
equalsIgnoreCase
(
"A"
)) {
Arrays
.
sort
(
names
);
System
.
out
.
println
(
"String array in Ascending Order: "
+
Arrays
.
toString
(
names
));
}
else
if
(
ch
.
equalsIgnoreCase
(
"B"
)) {
Arrays
.
sort
(
names
,
Collections
.
reverseOrder
());
System
.
out
.
println
(
"String array in Decending Order: "
+
Arrays
.
toString
(
names
));
}
System
.
out
.
println
(
"Enter the element to search: "
);
String
elem
=
s
.
nextLine
();
System
.
out
.
print
(
"Result: "
);
binarySearch
(
elem
,
names
);
}
public
static
void
binarySearch
(
String
element
,
String
[]
array
) {
int
index
=
0
;
while
(
index
<
array
.
length
) {
if
(
element
.
equalsIgnoreCase
(
array
[
index
])) {
System
.
out
.
println
(
element
+
" is found at the location of "
+
index
);
break
;
}
else
{
index
++;
}
if
(
index
==
array
.
length
) {
System
.
out
.
println
(
element
+
" is not found in the list.
\n
"
);
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL