Java selection sorting algorithm

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
+4
source share
3 answers

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

You had a different problem. Inside your sorting method:

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

check should be replaced by n .

Here is the 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
+4
source

Your program will not execute or will not display the correct result for some reason.

  • private static void sort (int [] values) , , . , .

  • . . n.

        for(int n=0; n < values.length; n++ )
        {
            System.out.print(values[scan] + " ");
        }
    
  • . . .

    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] + " ");
        }
    }
    }
    
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]+", ");
}
0
source

All Articles