File tree Expand file tree Collapse file tree 1 file changed +3
-5
lines changed Expand file tree Collapse file tree 1 file changed +3
-5
lines changed Original file line number Diff line number Diff line change @@ -8,8 +8,7 @@ public class BinarySearch {
88    public  static  void  main (String [] args ) {
99
1010        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" );
1312        System .out .println ("Enter total number of elements : " );
1413        int  length  = commandReader .nextInt ();
1514        int [] input  = new  int [length ];
@@ -19,8 +18,7 @@ public static void main(String[] args) {
1918            input [i ] = commandReader .nextInt ();
2019        }
2120
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)" );
2422        int  key  = commandReader .nextInt ();
2523
2624        int  index  = performBinarySearch (input , key );
@@ -66,7 +64,7 @@ public static int performBinarySearch(int[] input, int number) {
6664        int  high  = input .length  - 1 ;
6765
6866        while  (high  >= low ) {
69-             int  middle  = ( low  + high ) / 2 ;
67+             int  middle  = low  + ( high  -  low ) / 2 ;
7068            if  (input [middle ] == number ) {
7169                return  middle ;
7270            } else  if  (input [middle ] < number ) {
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments