Question
Java Project - No Binary Search & No Arraylist This project requires you to write an operation known as insert-in-order. You will start with an
Java Project - No Binary Search & No Arraylist
This project requires you to write an operation known as insert-in-order. You will start with an empty load that array with numbers you will read from a file. We have done this before. This time you will not append the new element to the end. Instead, you will splice that element into the array so that the array is always kept in sorted order. The first element into an empty array will always go into slot [0]. The next element would either go into slot [1] if that number is greater than the one at [0], or if the new number is less than the number at [0], the number at [0] has to be copied up one position to make room for the new number to be put into slot [0]. This process repeats until all the numbers from the file are loaded into the array and the array elements are in sorted order ascending.
The definition of in sorted order for an array is as follows:
The rules for the insertInOrder method you must fill in are as follows:
-
- an array is in sorted order if and only if there are no two elements out of order with respect to each other
- it follows from rule #1 that an empty array or an array with exactly one element is in sorted order
- The array must always be in array discipline (all values packed tight to the front and an accurate count variable) and in sorted order prior to any call to insertInOrder()
- The code inside InsertinOrder places exactly one new element into the array at the exact position that keeps the array sorted order.
- insertInOrder leaves with the array being in discipline and in sorted order
___________________________________________
// STARTER FILE PROJECT #7 CS 007 import java.util.*; import java.io.*; public class Project7 { static final int MAX_CAPACITY = 30; // HARDOCED PLENTY BIG. WE'LL DO TRIM AFTER public static void main( String args[] ) throws Exception { if (args.length < 1 ) { System.out.println("usage: java Project7 "); System.exit(0); } int[] arr = new int[ MAX_CAPACITY ]; int count=0; Scanner infile = new Scanner( new File( args[0] ) ); while ( infile.hasNextInt() ) { insertInOrder( arr, count, infile.nextInt() ); ++count; } arr = trimArray( arr, count ); printArray( arr ); // NOTE: NO COUNT PASSED IN }// END MAIN // ############################################################################################################ // MUST USE ENHANCED FOR LOOP IN THIS METHOD // (YOUR TRIM BETTER HAVE BEEN WRITEN CORRECTLY) static void printArray( int[] array ) { // PRINT EACH NUMBER WITH A SPACE AFTER IT System.out.println(); // LEAVE THIS HERE } static int[] trimArray( int[] array, int count ) { return array; // THIS IS NOT CORRECT. YOU MUST REPLACE IT!!!! } // THE CODE IN HERE NOW JUST APPENDS. THIS IS NOT CORRECT static void insertInOrder( int[] arr, int count, int newVal ) { arr[count] = newVal; // THIS IS NOT CORRECT. YOU MUST REPLACE IT!! } }// END PROJECT #7
_____________________________________________
*THIS IS THE INPUT FILE*
59 82 29 52 60 44 78 93 22 67 76 54 13 77 94 62 100 27 85 35 68 38 55 26 43
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started