Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to use Arrays library\'s built in sort and binary-search methods import java

ID: 3651818 • Letter: H

Question

How to use Arrays library's built in sort and binary-search methods


import java.io.*;
import java.util.*;


public class Lab5
{
public static void main( String[] args ) throws Exception
{
if (args.length < 1 )
{
System.out.println("CMD LINE INPUT ERROR: Must enter one filename on command line ");
System.exit(0);
}

Scanner infile1 = new Scanner( new File(args[0]) ); // load this file into the array then sort using Arrays.sort


int[] array = new int[20]; // Array can hold 20 ints - your input file will not fill the array
int arrCnt = 0; // num of values put into the array so far.

// READ THE NUMBERS FROM FILE1 INTO THE ARRAY. THEN SORT USING Arrays.sort

while ( infile1.hasNextInt() )
array[ arrCnt++ ] = infile1.nextInt();
infile1.close();
System.out.print("ORIGINAL ORDER " + args[0] + " " );
printArray( array, arrCnt ); // must come out in original order as in file1

// YOUR LINE OF CODE HERE (add below or replace this comment) THAT SORTS THE ARRAY USING Arrays LIBRARY

System.out.print("AFTER SORTING " + args[0] + " " );
printArray( array, arrCnt ); // must come out in sorted order


// WARNING: BINARY SEARCH WILL NOT WORK UNLESS ARRAY CORRECTLY SORTED

Scanner kbd = new Scanner( System.in );
do
{
System.out.print("Enter positive number to search for in array: ");
int key = kbd.nextInt();
if (key==0) break; // 0 is the user's signal to quit

// startInd INCLUSIVE but stopInd NOT inclusive, otherwise we would pass in cnt-1

// YOUR CODE HERE (below this comment) THAT SEARCHES THE ARRAY USING Arrays LIBRARY

int ind = -1; // replace -1 with a call to binarySearch // REMEMBER: stopInd not inclusive
if ( ind < 0 ) // a negative return value indicates the key was NOT found
System.out.println( key + " NOT FOUND");
else
System.out.println( key + " FOUND at index " + ind );

} while( true ); // this is an infinite loop - only way out is to BREAK out


} // END MAIN


// ########################################################################################

// USE THIS METHOD AS GIVEN: DO NOT CHANGE

private static void printArray( int[] arr, int cnt )
{
for( int i=0 ; i<cnt ;++i )
System.out.printf("%d ", arr[i] );
System.out.println();
}

} // END PROGRAM5

Explanation / Answer

import java.io.*;
import java.util.*;


public class Lab5
{
public static void main( String[] args ) throws Exception
{
if (args.length < 1 )
{
System.out.println("CMD LINE INPUT ERROR: Must enter one filename on command line ");
System.exit(0);
}

Scanner infile1 = new Scanner( new File(args[0]) ); // load this file into the array then sort using Arrays.sort


int[] array = new int[20]; // Array can hold 20 ints - your input file will not fill the array
int arrCnt = 0; // num of values put into the array so far.

// READ THE NUMBERS FROM FILE1 INTO THE ARRAY. THEN SORT USING Arrays.sort

while ( infile1.hasNextInt() )
array[ arrCnt++ ] = infile1.nextInt();
infile1.close();
System.out.print("ORIGINAL ORDER " + args[0] + " " );
printArray( array, arrCnt ); // must come out in original order as in file1

// YOUR LINE OF CODE HERE (add below or replace this comment) THAT SORTS THE ARRAY USING Arrays LIBRARY

Arrays.sort(array,0,arrCnt-1);

//Using Sort method of class Arrays which takes 3 Arguments

//Sort(int[] a,fromIndex,ToIndex)

// This method sorts the array from array[0] array[arrCnt-1]

//arrCnt-1 is used to specify last element index of array


System.out.print("AFTER SORTING " + args[0] + " " );
printArray( array, arrCnt ); // must come out in sorted order


// WARNING: BINARY SEARCH WILL NOT WORK UNLESS ARRAY CORRECTLY SORTED

Scanner kbd = new Scanner( System.in );
do
{
System.out.print("Enter positive number to search for in array: ");
int key = kbd.nextInt();
if (key==0) break; // 0 is the user's signal to quit

// startInd INCLUSIVE but stopInd NOT inclusive, otherwise we would pass in cnt-1

// YOUR CODE HERE (below this comment) THAT SEARCHES THE ARRAY USING Arrays LIBRARY

int ind = -1; // replace -1 with a call to binarySearch // REMEMBER: stopInd not inclusive

ind = Arrays.binarySearch(array,0,arrCnt-1,key);

//Using binarySearch method of Arrays which takes 4 Arguments

//binarySearch(int[] a,fromIndex,ToIndex,key)

// This method searches the array from array[0] array[arrCnt-1] for key

//arrCnt-1 is used to specify last element index of array

if ( ind < 0 ) // a negative return value indicates the key was NOT found
System.out.println( key + " NOT FOUND");
else
System.out.println( key + " FOUND at index " + ind );

} while( true ); // this is an infinite loop - only way out is to BREAK out


} // END MAIN


// ########################################################################################

// USE THIS METHOD AS GIVEN: DO NOT CHANGE

private static void printArray( int[] arr, int cnt )
{
for( int i=0 ; i<cnt ;++i )
System.out.printf("%d ", arr[i] );
System.out.println();
}

} // END PROGRAM5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote