Java:数组大小的插入排序麻烦

我正在尝试编写一个排序程序,该程序将询问用户要使用哪种类型的排序方法(插入,冒泡,选择),然后要求他输入要排序的整数.

我认为除了数组之外,我所有的东西都正确:我希望数组的大小与用户输入的整数数量一样大,但是我似乎做的并不正确.

在插入类方法所在的排序类中,应该将输入参数命名为那样(通过算法),还是应该将通用名称命名为“ arr”?

在哪里可以改善和更正我的代码?

谢谢你的帮助!!

DriverSort类:

import java.util.Scanner;

public class DriverSort 
{
    public static void main(String[] args) 
    {
        Scanner scan =new Scanner(System.in);
        Sorter sorter = new Sorter();

        int choice;// variable which says which sorting algorithm to use

        System.out.println("1-Insertion sort\n"
                    +"2-Selection sort\n"
                    + "3-Bubble sort\n"
                    + "0-quit\n");
        int size = scan.nextInt();
        int input[] = new int[size];

        System.out.println("Please enter the number for a sorting method or enter 0 to quit: ");
        size = scan.nextInt();


        System.out.println("\nBefore Sorting: ");
        sorter.printArray(input);

        // sort the array
        Sorter.insertionSort(input);
        System.out.println("\nAfter Sorting: ");
        sorter.printArray(input);

        switch (choice) 
        {
            case 0:
                System.out.println("Goodbye!");
                System.exit(0);
                break;

            case 1: 
                Sorter.insertionSort(input);
                sorter.printArray(input);
                break;
        }
   }
}

分类器类别:

public class Sorter
{
    public static int[] insertionSort(int[] input) 
    {
        for (int i = 1; i < input.length; i++) 
        {
            int valueToSort = input[i];
            int j = i;

            while (j > 0 && input[j - 1] > valueToSort) 
            {
                input[j] = input[j - 1];
                j--;
            }//end while loop.

            // insert the element
            input[j] = valueToSort;
        }//end for loop    

        return input;
    }//end insertionSort          

    public void printArray(int[] input) 
    { 
        System.out.println(input.toString());
    }
}   

解决方法:

我建议完全删除Sorter类,并将Sorter类的功能作为方法添加到DriverSorter中.我之所以这样说,是因为您实现Sorter的方式不会创建有用的实例.

import java.util.Scanner;

public class DriverSort {

    public static void main(String[] args) {

        Scanner scan =new Scanner(System.in);

        // this makes more sense to put this at the start of the program
        int choice;  // variable which says which sorting algorithm to use
        System.out.println("1-Insertion sort\n"
                +"2-Selection sort\n"
                + "3-Bubble sort\n"
                + "0-quit\n");
        choice = scan.nextInt();

        if (choice != 0) { // a simple if else statement will do just fine
            // must prompt user for the "input first"
            System.out.println("Enter the length vector to be modified: ");
            int size = scan.nextInt();
            // now actually get the vector
            int input[] = new int[size];
            for (int i = 0; i < size; i++) {
                System.out.println("Enter next array element: ");
                input[i] = scan.nextInt();
            }

            System.out.println("\nBefore Sorting: ");
            System.out.println(input); // use the builtin functionality
            // sort the array
            int[] output = insertionSort(input);
            System.out.println("\nAfter Sorting: ");
            System.out.println(output);
        } else { 
            System.out.println("Goodbye!");
            System.exit(0);
        }  
    }

    // returns a sorted list (add more detail here)
    // add a new input that tells what sort of sorting to do
    public static int[] insertionSort(int[] input) {

        for (int i = 1; i < input.length; i++) {
            int valueToSort = input[i];
            int j = i;
            while (j > 0 && input[j - 1] > valueToSort) {
                input[j] = input[j - 1];
                j--;
            }//end while loop.
            // insert the element
            input[j] = valueToSort;
        }//end for loop    
        return input;
    } 
}

保持分类器类别:

public class Sorter {
    private int vector; \\ private just means only things inside this class can affect this variable
    // now initializer 
    public Sorter(int[] input) {
        this.vector = input; \\ set our field to be equal to the vector you input when making an instance
    // so the call to make a Sorter object will now be "Sorter sorter = new Sorter(input);"
    }

    // make this act on vector instead
    public static int[] insertionSort() // no input because it can "see" our vector field
    {
        int[] copy = this.vector; // make a copy so you don't mess vector up before your finished
        for (int i = 1; i < copy.length; i++) 
        {
            int valueToSort = copy[i];
             int j = i;

            while (j > 0 && copy[j - 1] > valueToSort) 
            {
                copy[j] = copy[j - 1];
                j--;
            }//end while loop.

            // insert the element
            copy[j] = valueToSort;
        }//end for loop    

        this.vector = copy; // now replace old field with our sorted copy!
    }//end insertionSort          

    // this is an excellent way to be able to see "vector" without allowing other
    // mischievous programs to accidentally change "vector." This is very similar reasoning
    // to why you very frequently have fields be private. Read up on encapsulation - it's 
    // super useful.
    public void printArray(int[] input) 
    { 
    System.out.println(this.vector.toString());
    }

}

上一篇:参数不适用于python postgres


下一篇:Java servlet:request.getParameter()从POST请求中的查询字符串返回参数