简体   繁体   中英

Why is my selection sort program not giving the correct output for the last few numbers?

I've been going through this MOOC.fi Java course and got to the problem about developing a selection sort algorithm on this page ( https://java-programming.mooc.fi/part-7/2-algorithms ). I don't have the solutions, so I was wondering if anyone here could help me solve this problem. Basically, I got through all the steps uptil Part 4, but when I tried to make the selection sort method, my method only sorted the numbers correctly until it got to the second to last number, and then it started switching the numbers incorrectly. Can anyone look over my code and tell me where I went wrong?

import java.util.Arrays;
public class MainProgram {
    public static void main(String[] args) {
        // Testing methods
        // Test Part 1
        int[] numbers = {6, 5, 8, 7, 11};
        System.out.println("Smallest: " + MainProgram.smallest(numbers));
        // Testing Part 2
        System.out.println("Index of the smallest number: " + MainProgram.indexOfSmallest(numbers));
        // Testing Part 3
        System.out.println(MainProgram.indexOfSmallestFrom(numbers, 0));
        System.out.println(MainProgram.indexOfSmallestFrom(numbers, 1));
        System.out.println(MainProgram.indexOfSmallestFrom(numbers, 2));
    // Testing Part 4
    int[] numbers2 = {3, 2, 5, 4, 8};

    System.out.println(Arrays.toString(numbers2));

    MainProgram.swap(numbers2, 1, 0);
    System.out.println(Arrays.toString(numbers2));

    MainProgram.swap(numbers2, 0, 3);
    System.out.println(Arrays.toString(numbers2));

    // Testing Part 5
    int[] numbers3 = {8, 3, 7, 9, 1, 2, 4};
    MainProgram.sort(numbers3);
}
// Part 1
public static int smallest(int[] array) {
    int smallest = array[0];
    for (int i = 0; i < array.length; i++) {
        if (array[i] < smallest) {
            smallest = array[i];
        }
    }
    return smallest;
}
// Part 2
public static int indexOfSmallest(int[] array){
    int smallest = array[0];
    int i;
    int finalIndex = 0;
    for (i = 0; i < array.length; i++) {
        if (array[i] < smallest) {
            smallest = array[i];
            finalIndex = i;
        }
    }
    return finalIndex;
}
// Part 3
public static int indexOfSmallestFrom(int[] table, int startIndex) {
    int smallest = table[startIndex];
    int i = startIndex;
    int finalIndex = 0;
    for (i = startIndex; i < table.length; i++) {
        if (table[i] < smallest) {
            smallest = table[i];
            finalIndex = i;
        }
    }
    return finalIndex;
}
// Part 4
public static void swap(int[] array, int index1, int index2) {
    int temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;
}
// Part 5
public static void sort(int[] array) {
    int smallestIndex;
    for (int i = 0; i < array.length; i++) {
        smallestIndex = indexOfSmallestFrom(array, i);
        swap(array, smallestIndex, i);
        System.out.println(Arrays.toString(array));
    }
}

}

Here is the wrong output:

[1, 3, 7, 9, 8, 2, 4]
[1, 2, 7, 9, 8, 3, 4]
[1, 2, 3, 9, 8, 7, 4]
[1, 2, 3, 4, 8, 7, 9]
[1, 2, 3, 4, 7, 8, 9]
[8, 2, 3, 4, 7, 1, 9]
[9, 2, 3, 4, 7, 1, 8]

In the 3 block you set the finalIndex = 0 ;

It should be set to the startIndex

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM