write a Java program that sorts a sequence of numbers using two different algori
ID: 3651503 • Letter: W
Question
write a Java program that sorts a sequence of numbers using two different algorithms: insertInOrder and bubbleSort.input file 1 - 12 23 5 17 6 19 13 3 41 21 11 22 30 31 16
input file 2 - 58 120 121 69 138 171 36 175 31 115 104 22
import java.io.*;
import java.util.*;
public class Program5
{
private static final int NOT_FOUND = -1;
public static void main( String[] args ) throws Exception
{
if (args.length < 2 )
{
System.out.println("CMD LINE INPUT ERROR: Must enter two filenames on command line ");
System.exit(0);
}
Scanner infile1 = new Scanner( new File(args[0]) ); // fill an array from this file using insertInOrder
Scanner infile2 = new Scanner( new File(args[1]) ); // fill an array from this file using selectionSort
int[] array = new int[20]; // Array can hold 20 ints - your input files 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 USING INSERT IN ORDER
System.out.print("ORIGINAL ORDER " + args[0] + " " );
while ( infile1.hasNextInt() )
{
int n = infile1.nextInt();
System.out.print( n + " " ); // echo the numbers as read from the file
insertInOrder( array, arrCnt, n ); // insert number into the array at the position it belongs
arrCnt++; // update counter
}
infile1.close();
System.out.println();
System.out.print("INSERT IN ORDER " + args[0] + " " );
printArray( array, arrCnt ); // must come out in sorted order
// READ THE NUMBERS FROM FILE2 INTO THE ARRAY. THEN SORT USING BUBBLE SORT
arrCnt = 0; // very important to reset the count since we are re-using the same array space
while ( infile2.hasNextInt() )
array[ arrCnt++ ] = infile2.nextInt();
infile2.close();
System.out.print("ORIGINAL ORDER " + args[1] + " " );
printArray( array, arrCnt ); // must come out in original order as in file2
// now sort array after loading it
bubbleSort( array, arrCnt );
System.out.print("BUBBLE SORT " + args[1] + " " );
printArray( array, arrCnt ); // must come out in sorted order
} // 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();
}
// YOU WRITE THIS METHOD
private static void insertInOrder( int[] arr, int cnt, int n )
{
// YOUR CODE HERE
}
// YOU WRITE THIS METHOD
private static void bubbleSort( int[] arr, int cnt )
{
//YOUR CODE HERE
}
} // END PROGRAM5
Explanation / Answer
public class sort{ public static void main(String a[]){ int i; int input1[] = {12,23,5,17,6,19,13,3,41,21,11,22,30,31,16}; int input11[] = {12,23,5,17,6,19,13,3,41,21,11,22,30,31,16}; int input2[] = {58,120,121,69,138,171,36,175,31,115,104,22}; int input22[] = {58,120,121,69,138,171,36,175,31,115,104,22}; System.out.println("Input File 1 - 12 23 5 17 6 19 13 3 41 21 11 22 30 31 16"); System.out.println(); System.out.println("Selection Sort:"); insertionSort(input1, input1.length); for(i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.