Search code examples
javaarraysselection-sort

Selection sort not working properly with zero or negative inputs


Im not really sure why the selection sort isn't working with negative numbers and zeros in the array. It works perfectly fine with all positive integers. Feedback would be greatly appreciated.

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

Selection sort method along with the class for the project:

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

Thanks & I apologize for all the edits. I never use online forums.


Solution

  • Try doing swapping outside inner for loop :-

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

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

    input : 4,0,-4,5,2,1,

    Array after sorrting:

    Number of digits in array: 6

    Digits in array: -4 0 1 2 4 5

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

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

    Array after sorrting:

    Number of digits in array: 12

    Digits in array: -43 -12 -7 2 3 3 3 4 5 21 22 26