Question: In JAVA For this problem, do NOT use any class that manipulates data in a collection like the Arrays class or the Collection class Write

In JAVA

For this problem, do NOT use any class that manipulates data in a collection like the Arrays class or the Collection class

Write a program that accepts as its first input a number that represents the number of strings to be inserted followed by the Strings. The program should insert the strings into an array in its sorted position. The array should NOT be sorted after all elements are inserted rather it should be sorted after each individual element is inserted. Once all elements have been inserted print out the sorted array. Sort in ASCENDING order (A-Z).

You should have 2 methods that do the following:

1)Prints the array

2)Places element in its sorted position

First input is the number of elements to be inserted. On each new line is a string to be inserted. Output Format A single line with the elements printed in ascending order separated by a comma. Sample Input Number of elements: 3 Element 1: Bravo Element 2: Alpha Element 3: Charlie Sample Output Alpha,Bravo,Charlie Hints One method of solving this involves putting the element to be inserted at the end spot, not the end of the array, but the next position to be inserted. Check the inserted elements left neighbor, index-1, and if it is greater than the inserted element swap them. Do this until the element is in its inserted position which is when it is greater than its left neighbor.

Using sample input from above: Bravo Alpha Charlie Array of size 3 [,,] Insert index = 0, Insert element = Bravo Array = [Bravo] It is in sorted order Insert index = 1, Insert element = Alpha Array = [Bravo, Alpha] Check Alphas left neighbor Alpha.compareTo(Bravo) < 0 Result returns true so Alpha is less than Bravo Swap Bravo and Alpha Array = [Alpha, Bravo] Insert index = 2, Insert element = Charlie Array = [Alpha, Bravo, Charlie] Check Charlies left neighbor Charlie.compareTo(Bravo) < 0 Result returns false so Charlie is greater than Bravo No swapping occurs Print Array

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!