Java中的选择排序算法 [英] Selection sort Algorithm in Java

查看:70
本文介绍了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.

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

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.

我创建的排序方法出错.我收到语法错误,并且void是无效的类型.我想我缺少了一些东西,我不确定如何解决此问题.如果有人可以指出我正确的方向.任何帮助将不胜感激.

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

推荐答案

您不能在main内部使用其他方法.从main中获取方法sort(int [] values),然后在main中调用它.

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

扫描必须替换为 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

这篇关于Java中的选择排序算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆