简体   繁体   English

Java中的选择排序算法

[英]Selection sort Algorithm in Java

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. 数组的大小取决于用户输入的值,因此它设置为变量numValues而不是数字。

I get an error with the sort method I created. 我使用我创建的排序方法出错。 I'm getting syntax errors and void is an invalid type. 我收到语法错误, void是无效类型。 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

You cannot have another method inside main. 你不能在main里面有另一种方法。 Get the method sort(int[] values) out of main, and call it inside main. 从main中获取方法sort(int [] values),并在main中调用它。

You had another problem. 你有另一个问题。 Inside your sort method: 在排序方法中:

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

scan has to be replaced by n . 扫描必须由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

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. 您在main方法中使用“private static void sort(int [] values)”方法,但这是不可能的,因为我们无法在另一个方法中定义方法。 Either you have to create a separate method outside of main method or you can use sorting functionality in your main method also. 要么必须在main方法之外创建单独的方法,要么也可以在main方法中使用排序功能。

  2. Another mistake is in below code. 另一个错误在于下面的代码。 You are using scan variable for displaying result in ascending order. 您正在使用扫描变量以升序显示结果。 Here is should be n. 这应该是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] + " "); } } } 
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]+", ");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM