4

I'm having some trouble sorting an array. I'm trying to sort it in ascending order.

My task is to get a series of integers from the user and store them into an array, then display them back to the user in ascending order. I was fine getting input from the user, storing it in the array and displaying them back. I was able to run my code and get the results I wanted but as far as getting the integers in the array in ascending order using selection sort, I was having a lot of difficulty with that.

The size of the array depends on the value that the user inputs, so it is set to the variable numValues rather than a number.

I get an error with the sort method I created. I'm getting syntax errors and void is an invalid type. I think I'm missing something and I'm not sure how to go about fixing this. 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
5
  • I didn't really dive into your code, but you should think about using the built-in Arrays.sort. Commented Jul 9, 2015 at 23:43
  • Welcome to SO. Please limit the code you post to the bit that your question relates to. As you say, you can do everything successfully but sort it - so please trim your code down to your sorting routine. Commented Jul 9, 2015 at 23:43
  • I don't see anything obviously wrong with your code. can you detail the issues you are having? Commented Jul 9, 2015 at 23:45
  • I've cut down my code to only the part where there is an issue Commented Jul 9, 2015 at 23:59
  • You have a the sort method inside the java main method. Extract your sort method. Commented Jul 10, 2015 at 0:01

3 Answers 3

4

You cannot have another method inside main. Get the method sort(int[] values) out of main, and call it inside main.

You had another problem. Inside your sort method:

System.out.print(values[scan] + " ");

scan has to be replaced by n.

Here is the completed code:

import java.util.*;

public class Project {

    public static void main(String[] args) {


        int numValues;         // The number of values user has
        int [] values;         // Array declaration for values


        Scanner keyboard = new Scanner(System.in);             // Scanner object to get input from user

        System.out.println("How many values do you have?");    // To get number of values for array
        numValues = keyboard.nextInt();


        /*
         * Array to hold number of values
         */
        values = new int [numValues];


            /*
             * Loop to gather integer values
             */
        for (int n=0; n < values.length; n++ )
        {

            System.out.print("Enter value " + (n+1) + ":" );
            values[n] = keyboard.nextInt();

        } //End for loop
        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");
        sort(values);
        keyboard.close();    // To close Scanner object
    }
            /*
             * 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[n] + " ");
        } //End for

    } // End method sort
} // End class Project
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! that worked. The main issue was I had my method inside main. I made those changes and now it works as it should.
0

Your program will not executed or will not display correct result due to some reasons.

  1. You are using "private static void sort(int[] values)" method in main method and it's not possible because we can't define method in another method. Either you have to create a separate method outside of main method or you can use sorting functionality in your main method also.

  2. Another mistake is in below code. You are using scan variable for displaying result in ascending order. Here is should be n.

        for(int n=0; n < values.length; n++ )
        {
            System.out.print(values[scan] + " ");
        }
    
  3. Your logic is correct. But it's little bit large for selection sort. Below is small way to do this.

    public class SelectionSort
    {
    public static void main(String[]args)
    {
        int [] values = {15,14,13,12,11};
        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("\nHere are the values you've entered, in ascending order");
            sort(values);
    }
    private static void sort(int[] values)
    {
    
     int index = 0;
     int index2 = 0;
     int temp = 0;
     for(index=0; index<values.length; index++)
     {
         for(index2 = index+1; index2< values.length; index2++)
         {
                if(values[index] > values[index2])
                {
                    temp = values[index];
                    values[index]= values[index2];
                    values[index2] = temp;
                }
         }
     }
    
        for(int n=0; n < values.length; n++ )
        {
            System.out.print(values[n] + " ");
        }
    }
    }
    

Comments

0
int[] arrayToSort=new int[]{1,7,81,2,-2,9,9,6,-6};
//the outer loop will switch the number

for(int i=0;i<arrayToSort.length;i++){
    int indexSmal=i;
    //the inner loop will search for the biggest number
    for(int j=i+1;j<arrayToSort.length;j++){
        //search for biggest number index starting from i index
        if(arrayToSort[j]>arrayToSort[indexSmal]){
           indexSmal=j;
        }
    }//end loop

        //swap the number
        int smallNum=arrayToSort[indexSmal];
        arrayToSort[indexSmal]=arrayToSort[i];
        arrayToSort[i]=smallNum;

    }// end loop 
    for(int i=0;i<arrayToSort.length;i++){
        System.out.print(arrayToSort[i]+", ");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.