Here is my assignment.
Use the code samples that you created in class for this project.
Load the following items of data into an array interactively from the keyboard.
Print out the data in the order in which is was entered.
Sort the data in ascending order using the insertion sort.
Print out the data in sorted order.
Sort the data in descending order using the bubble sort.
Print out the data in the sorted order.
These are the data items you’ll use:
Honda
Chevrolet
Ford
Rolls-Royce
Mercedes Benz
I'm having trouble with the insertion method when running my methods in my main method. Here is my output.
Hello user, I will sort your list in asscending and descending order?
You will need to input 5 strings. Please press enter for each string.
Please enter your string.
Honda
Chevrolet
Ford
Rolls-Royce
Mercedes Benz
Honda
Here is my code
import java.util.*; public class Project7 { public static void main (String[] args) { Scanner scan = new Scanner(System.in); String[] storedArray = new String[5]; int size = 5; int i; System.out.println( "Hello user, I will sort your list in asscending and descending order? "); System.out.println( "You will need to input 5 strings. Please press enter for each string." ); System.out.println( "Please enter your string." ); for(i = 0; i < storedArray.length; i++) { storedArray[i] = scan.nextLine(); } insertionSort(storedArray,size); printArray(storedArray); } public static void insertionSort(String[] array, int length) { int i; for( i = 1; i<length; i++) acsendinginsert(array,i); } public static void acsendinginsert(String[] x, int i) { String temp = " "; int j, result, result1; j = i-1; result = x[i].compareToIgnoreCase(x[j]); result1 = x[j].compareToIgnoreCase(x[i]); while(j >= 0 && result < result1 ) { x[j+1] = x[j]; j--; } x[j+1] = temp; } public static void printArray(String displayed []) { int i; for(i = 0; i < displayed.length; i++) { System.out.println(displayed[i]); } } }
For some reason my output is only returning one string instead of my list of order strings. Is there any suggestions?