【问题标题】:Selection sort Algorithm in JavaJava中的选择排序算法
【发布时间】:2016-06-13 22:57:43
【问题描述】:

我在排序数组时遇到了一些问题。我正在尝试按升序对其进行排序。

我的任务是从用户那里获取一系列整数并将它们存储到一个数组中,然后按升序将它们显示给用户。我很好地从用户那里获得输入,将其存储在数组中并显示回来。我能够运行我的代码并获得我想要的结果,但就使用选择排序按升序排列数组中的整数而言,我遇到了很多困难。

数组的大小取决于用户输入的值,因此设置为变量numValues而不是数字。

我创建的排序方法出错。我收到语法错误,void 是无效类型。我想我错过了一些东西,我不确定如何解决这个问题。如果有人能指出我正确的方向。任何帮助将不胜感激。

    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

【问题讨论】:

  • 我并没有真正深入研究您的代码,但您应该考虑使用内置的Arrays.sort
  • 欢迎来到 SO。请将您发布的代码限制为与您的问题相关的位。正如您所说,您可以成功地完成所有操作,但要对其进行排序 - 所以请将您的代码缩减为您的排序例程。
  • 我看不出您的代码有任何明显错误。您能详细说明您遇到的问题吗?
  • 我已将代码缩减为仅存在问题的部分
  • 您在 java main 方法中有一个 sort 方法。提取您的排序方法。

标签: java arrays sorting


【解决方案1】:

main 中不能有其他方法。从 main 中获取方法 sort(int[] values),并在 main 中调用它。

您遇到了另一个问题。在您的排序方法中:

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

scan 必须替换为 n

这是完整的代码:

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

【讨论】:

  • 谢谢!那行得通。主要问题是我在 main.js 中有我的方法。我进行了这些更改,现在它可以正常工作了。
【解决方案2】:

由于某些原因,您的程序将无法执行或无法显示正确的结果。

  1. 您在 main 方法中使用了“private static void sort(int[] values)”方法,这是不可能的,因为我们不能在另一个方法中定义方法。您必须在主方法之外创建一个单独的方法,或者您也可以在主方法中使用排序功能。

  2. 另一个错误是在下面的代码中。您正在使用扫描变量以升序显示结果。这里应该是n。

        for(int n=0; n < values.length; n++ )
        {
            System.out.print(values[scan] + " ");
        }
    
  3. 你的逻辑是正确的。但它对于选择排序来说有点大。以下是执行此操作的小方法。

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

【讨论】:

    【解决方案3】:
    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]+", ");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      • 2021-03-20
      • 2019-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-01
      相关资源
      最近更新 更多