选择排序与零输入或负输入无法正常工作

时间:2017-10-23 16:44:37

标签: java arrays selection-sort

我不确定为什么选择排序不能与数组中的负数和零一起使用。它与所有正整数完美配合。反馈将不胜感激。

    private static void selectionSort(int[] digits) {

    for (int i = 0; i < digits.length - 1; i++) {
        int index = i;
        for (int j = i + 1; j < digits.length; j++) {
            if (digits[j] < digits[index]) {
                index = j;
            }
        int min = digits[index];
        digits[index] = digits[i];
        digits[i] = min;    
        }
    }
    System.out.println("Array after sorrting:");
    System.out.println("Number of digits in array: " + digits.length);
    System.out.print("Digits in array: ");
    for (int i: digits) {
        System.out.print(i + " ");
    }
    System.out.println("\n");
}

选择排序方法以及项目的类:

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    int n = 0;
    do {
        n = getNumDigits(in);
        if (n != 0) {
            int[] digits = new int[n];
            getDigits(digits, in);
            displayDigits(digits);
            selectionSort(digits);
        }
    } while (n != 0);
    in.close();
    System.out.println("No digits to sort? Goodbye! ");
}

// Given a Scanner as input, prompts the user for the number of digits they will
// be
// entering into an array. If the number given by the user is less than 0,
// display
// an error message and ask for a number that is 0 or greater. When a valid
// number is
// received, return it to the calling program.
private static int getNumDigits(Scanner inScanner) {
    int digits = 0;
    do {
        System.out.print("Please enter the number of digits to be stored: ");
        digits = inScanner.nextInt();
        if (digits < 0) {
            System.out.println("ERROR!  You must enter a non-negative number of digits!\n");
        }
    } while (digits < 0);
    return digits;
}

// Given an array and a Scanner as input, prompt the user to input integers to
// fill the
// array. The procedure should display a prompt for the user to enter an
// integer, and
// should loop until the entire array is filled with integer.
private static void getDigits(int[] digits, Scanner inScanner) {
    int i = 0;
    while (i < digits.length) {
        System.out.print("Enter integer " + i + ": ");
        digits[i] = inScanner.nextInt();
        i++;
    }

}

// Given an array as input, displays the total number of digits contained in the
// array
// and displays the contents of the array in order, starting at index 0 and
// ending
// with the final index of the array.
private static void displayDigits(int[] digits) {

    System.out.println("\nArray before sorrting:");
    System.out.println("Number of digits in array: " + digits.length);
    System.out.print("Digits in array: ");
    for (int i = 0; i < digits.length; i++) {
        System.out.print(digits[i] + " ");
    }
    System.out.println("\n");
}

// FOR LAB10B
// Given an array of integers as input, sorts the array using the Selection Sort
// algorithm
// provided in the Closed Lab 10 write-up.
private static void selectionSort(int[] digits) {

    for (int i = 0; i < digits.length - 1; i++) {
        int index = i;
        for (int j = i + 1; j < digits.length; j++) {
            if (digits[j] < digits[index]) {
                index = j;
            }
        int min = digits[index];
        digits[index] = digits[i];
        digits[i] = min;    
        }
    }
    System.out.println("Array after sorrting:");
    System.out.println("Number of digits in array: " + digits.length);
    System.out.print("Digits in array: ");
    for (int i: digits) {
        System.out.print(i + " ");
    }
    System.out.println("\n");
}

谢谢&amp;我为所有的编辑道歉。我从不使用在线论坛。

1 个答案:

答案 0 :(得分:1)

尝试在内部for循环之外进行交换: -

private static void selectionSort(int[] digits) {

        for (int i = 0; i < digits.length - 1; i++) {
            int index = i;
            for (int j = i + 1; j < digits.length; j++) {
                if (digits[j] < digits[index]) {
                    index = j;
                }
               //previously u were doing swapping here
            //System.out.println("inner : " + convert(digits));
            }
            //i shifted the swapping block to this place
            int min = digits[index];
            digits[index] = digits[i];
            digits[i] = min; 

            //System.out.println("outer : " + convert(digits));
        }
        System.out.println("Array after sorrting:");
        System.out.println("Number of digits in array: " + digits.length);
        System.out.print("Digits in array: ");
        for (int i: digits) {
            System.out.print(i + " ");
        }
        System.out.println("\n");
    }

============

输入:4,0,-4,5,2,1,

摘录后的数组:

数组中的位数:6

数组中的数字:-4 0 1 2 4 5

============

输入:3,4,2,5,3,21,-7,3,26,-43,-12,22,

摘录后的数组:

数组中的位数:12

阵列中的数字:-43 -12 -7 2 3 3 3 4 5 21 22 26