Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will fill in the code for method insertInOrder and the upSize() method at the bottom of your starter file. Main is written for you.

You will fill in the code for method insertInOrder and the upSize() method at the bottom of your starter file. Main is written for you. Every time you read another number from the input file, you will pass that new value into insertInOrder. It will place the new number into the array in it's sorted place. To figure out where the new number belongs you must compare it to the last number in the array. If the new value is smaller than that element, the end element must move up one to the right. You then keep moving toward the front of the array one step at a time comparing the new value to the next element. If the new value is smaller than it, you must push that element up one place. You keep doing this until the new value is not smaller than the one you are comparing it to. At this point you can now copy the new value into the hole you just opened up in the array.

At no time is the array allowed to be in un-sorted order. You are not allowed to copy the new value into the array except the one time when you copy it into its proper sorted place. You are not allowed to create another array. You are not allowed to just fill up the array and then call Arrays.sort() or write some sorting loop on it after it's full. The array must be in sorted order at all times.

import java.util.*;

import java.io.*;

public class Lab3

{

static final int INITIAL_CAPACITY = 5;

public static void main( String args[] ) throws Exception

{

if (args.length < 1 )

{

System.out.println(" usage: C:\\> java Lab3 L3input.txt ");

System.exit(0);

}

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 = upSize( arr );

insertInOrder( arr, count, infile.nextInt() );

++count;

}

infile.close();

printArray( "SORTED ARRAY: ", arr, count );

}

static void printArray( String caption, int[] arr, int count )

{

System.out.print( caption );

for( int i=0 ; i

System.out.print(arr[i] + " " );

System.out.println();

}

static void insertInOrder( int[] arr, int count, int key )

{

//YOUR CODE HERE

}

static int[] upSize( int[] fullArr )

{

// YOUR CODE HERE

return null; // CHANGE TO YOUR RETURN STATEMENT

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions