I'm having trouble sorting an array. I am trying to sort it in ascending order.
My task is to get a series of integers from the user and store them in an array, and then display them back to the user in ascending order. I really liked to receive information from the user, store it in an array and display it back. I managed to run my code and get the results that I wanted, but as far as they were received by integers in the array in ascending order using sorting, it was difficult for me to handle this.
The size of the array depends on the value that the user enters, so it is set by a variable numValues, not a number.
I get an error with the sort method I created. I am getting syntax errors, but voidan invalid type. I think I am missing something and I don’t know how to fix it. If someone can point me in the right direction. Any help would be appreciated.
System.out.println("Here are the values you've entered" );
for(int n=0; n<values.length; n++)
{
System.out.print(values[n] + "");
}
System.out.println("Here are the values you've entered, in ascending order");
/*
* Method to arrange values in ascending order
*/
private static void sort(int[] values) {
int scan;
int index;
int minIndex;
int minValue; // Variables to put values in ascending order
for(scan=0; scan < (values.length-1); scan++)
{
minIndex = scan;
minValue = values[scan];
for(index = scan+1; index < values.length; index++)
{
if(values[index] < minValue)
{
minValue = values[index];
minIndex = index;
} // End if
} //End for
values[minIndex] = values[scan];
values[scan] = minValue;
} // End for loop
/*
* For loop to display values
*/
for(int n=0; n < values.length; n++ )
{
System.out.print(values[scan] + " ");
} //End for
} // End method sort
keyboard.close(); // To close Scanner object
} //End method main
source
share