File tree 1 file changed +3
-5
lines changed
1 file changed +3
-5
lines changed Original file line number Diff line number Diff line change @@ -8,8 +8,7 @@ public class BinarySearch {
8
8
public static void main (String [] args ) {
9
9
10
10
Scanner commandReader = new Scanner (System .in );
11
- System .out .println ("Welcome to Java Program to perform
12
- binary search on int array" );
11
+ System .out .println ("Welcome to Java Program to perform binary search on int array" );
13
12
System .out .println ("Enter total number of elements : " );
14
13
int length = commandReader .nextInt ();
15
14
int [] input = new int [length ];
@@ -19,8 +18,7 @@ public static void main(String[] args) {
19
18
input [i ] = commandReader .nextInt ();
20
19
}
21
20
22
- System .out .println ("Please enter number to be searched in array
23
- (sorted order)" );
21
+ System .out .println ("Please enter number to be searched in array (sorted order)" );
24
22
int key = commandReader .nextInt ();
25
23
26
24
int index = performBinarySearch (input , key );
@@ -66,7 +64,7 @@ public static int performBinarySearch(int[] input, int number) {
66
64
int high = input .length - 1 ;
67
65
68
66
while (high >= low ) {
69
- int middle = ( low + high ) / 2 ;
67
+ int middle = low + ( high - low ) / 2 ;
70
68
if (input [middle ] == number ) {
71
69
return middle ;
72
70
} else if (input [middle ] < number ) {
You can’t perform that action at this time.
0 commit comments