title: bSearch java program
body: <p>I don't know what it wrong with my code. Im supposed to put in an input file and sort the numbers from lowest to greatest. When i run the program it returns:Sorted array of 25 ints from P4input.txt after insertInOrder:
  31 5 8 8 19 23 25 27 27 31 69 70 71 75 90 98 103 103 109 140 145 145 153 157 162</p>
  
  <p>I don't know why the first number is out of order.</p>
  
  <pre><code>/* Project4.java  InsertInOrder with bSearch optimization to compute insertion     index */
  
  import java.util.*;
  import java.io.*;
  
  public class Project4
  {
  static final int INITIAL_CAPACITY = 5;
  
  public static void main( String args[] ) throws Exception
  {
      // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE
      if (args.length &lt; 1 )
      {
          System.out.println("\nusage: C:\\&gt; java Project4 &lt;input filename&gt;\n\n"); // i.e. C:\&gt; java Project4 P4input.txt
          System.exit(0);
      }
  
      // LOAD FILE INTO ARR USING INSERINORDER
  
      Scanner infile =  new Scanner( new File( args[0] ) );
      int[] arr = new int[INITIAL_CAPACITY];
      int count= 0;
      while (infile.hasNextInt())
      {
          if ( count==arr.length )
                  arr = upSizeArr(arr);
          insertInOrder( arr, count++, infile.nextInt() );
      }
      infile.close();
      arr=trimArr(arr,count); // Now count == .length
      System.out.println( "Sorted array of " + arr.length + " ints from " + args[0] + " after insertInOrder:" );
      printArray( arr );  // we trimmed it thus count == length so we don't bother to pass in count
  
  } // END MAIN
  // ############################################################################################################
  
  // USE AS IS - DO NOT MODIFY
  static void printArray( int[] arr  )
  {
      for( int i=0 ; i&lt;arr.length ;++i )
          System.out.print(arr[i] + " " );
      System.out.println();
  }
  
  // USE AS IS - DO NOT MODIFY
  static int[] upSizeArr( int[] fullArr )
  {
      int[] upSizedArr = new int[ fullArr.length * 2 ];
      for ( int i=0; i&lt;fullArr.length ; ++i )
          upSizedArr[i] = fullArr[i];
      return upSizedArr;
  }
  
  // USE AS IS - DO NOT MODIFY
  static int[] trimArr( int[] oldArr, int count )
  {
      int[] trimmedArr = new int[ count ];
      for ( int i=0; i&lt;count ; ++i )
          trimmedArr[i] = oldArr[i];
      return trimmedArr;
  }
  
  // ############################################################################################################
  static void insertInOrder( int[] arr, int count, int key   )
  {
      int index=bSearch( arr, count, key ); // LEAVE THIS HERE
      if (arr[arr.length - 1] == 0)
      {
      for (int i = count; i &gt;= index + 1; i--) 
      {
      arr[i] = arr[i - 1];
      }
      arr[index]=key; // LEAVE THIS HERE
  }
  }
  static int bSearch(int[] a, int count, int key)
  {
      int hi = count-1;
      int lo = 0;
      int mid = 0;
  
      if(hi == -1) 
      {
          return lo;
       } 
       else
       {
       mid = (hi+lo)/2;
       }
      while (lo &lt;= hi) 
      {
      if (key==a[mid])
      {
          return (mid+1);
      }
      else if (key &lt; a[mid])
      {
          hi = mid-1;
          mid = (hi+lo)/2;
      }
      else 
      {
          lo = mid +1;
          mid = (hi+lo)/2;
      }
  }
      return (mid +1);
  }       
  
  } // END PROJECT4
  </code></pre>