Toggle navigation
Home
Latest pastes
FAQ
Random
BitBin is shutting down!
Register
Login
Java program for recursive binary sort
SUBMITTED BY:
Guest
DATE:
Dec. 25, 2013, 8:04 a.m.
FORMAT:
Java
SIZE:
979 Bytes
Raw
Download
Tweet
HITS:
1580
Go to comments
Report
import
java.util.Scanner
;
class
Binary
{
int
A
[]
;
int
n
,
l
,
u
;
public
Binary
(
int
nn
)
{
n
=
nn
;
A
=
new
int
[
n
]
;
l
=
0
;
u
=
n
-
1
;
}
public
void
readdata
()
{
Scanner
in
=
new
Scanner
(
System
.
in
);
System
.
out
.
println
(
"Enter the elments of the array in ascending order: "
);
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
A
[
i
]=
in
.
nextInt
();
}
}
public
int
binarysearch
(
int
v
)
{
if
(
l
>
u
)
return
-
1
;
else
{
int
mid
=
(
l
+
u
)
/
2
;
if
(
A
[
mid
]==
v
)
return
mid
+
1
;
else
if
(
v
<
A
[
mid
]
)
{
u
=
mid
-
1
;
return
binarysearch
(
v
);
}
else
{
l
=
mid
+
1
;
return
binarysearch
(
v
);
}
}
}
}
Please enable JavaScript to view the
comments powered by Disqus.
comments powered by
Disqus