Question: i am finishing a project for my cs 0401 java course. I have completed everything for the project that requires us to sort and then

i am finishing a project for my cs 0401 java course. I have completed everything for the project that requires us to sort and then using a binary search display a txt file read through the command prompt. For some reason when the program treads through the file the first element isnt sorted and just ends up being printed in front of all the other sorted elements.

my code is:

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 < 1 ) { System.out.println(" usage: C:\\> java Project4 "); // i.e. C:\> 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

// USE AS IS - DO NOT MODIFY static int[] upSizeArr( int[] fullArr ) { int[] upSizedArr = new int[ fullArr.length * 2 ]; for ( int i=0; i

// USE AS IS - DO NOT MODIFY static int[] trimArr( int[] oldArr, int count ) { int[] trimmedArr = new int[ count ]; for ( int i=0; i

// ############################################################################################################ 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 >= 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 = (hi +lo)/2; if(hi == -1) { return mid; } else { mid = (hi+lo)/2; } while (lo <= hi) { if (key==a[mid]) { return (mid+1); } else if (key < a[mid]) { hi = mid-1; mid = (hi+lo)/2; } else { lo = mid +1; mid = (hi+lo)/2; } } return (mid +1); }

}

and the .txt file has the elements:

31 145 70 5 27 103 71 140 8 162 98 153 8 109 103 31 145 157 27 90 75 19 23 25 69 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!