Java:数组大小的插入排序问题

时间:2014-08-10 00:53:34

标签: java arrays sorting methods parameters

我正在尝试编写一个排序程序,它会询问用户使用什么类型的排序方法(插入,冒泡,选择),然后让他输入整数进行排序。

我认为除了数组之外我还有一切正确:我希望数组的大小与用户输入的整数数一样大,但我似乎没有正确地做到这一点。

在insertSort方法所在的排序类中,我是否应该使用这样的输入参数(通过算法)或者我应该创建像“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());
    }
}   

2 个答案:

答案 0 :(得分:2)

我建议完全删除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());
    }

}

答案 1 :(得分:1)

我看到的第一件事是

int size = input.length;
int input[] = new int[size];

这是非法的我停止阅读。硬编码大小,或提示用户使用它。

int size = 10; // <-- 10
int input[] = new int[size]; // <-- this is fine.

int input[] = new int[10]; // <-- 10
int size = input.length; // <-- this if also fine.

所以,你想要像 -

这样的东西
System.out.println("Please enter the number for a sorting "
    + "method or enter 0 to quit: ");
int choice = scan.nextInt();
System.out.println("How many numbers do you want to enter: ");
int size = scan.nextInt();
int input[] = new int[size];