簡體   English   中英

為什么我的選擇排序程序沒有為最后幾個數字提供正確的 output?

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

我一直在學習這個 MOOC.fi Java 課程,並遇到了關於在這個頁面上開發選擇排序算法的問題( https://java-programming.mooc.fi/part-7/2-algorithms )。 我沒有解決方案,所以我想知道這里是否有人可以幫助我解決這個問題。 基本上,我完成了第 4 部分之前的所有步驟,但是當我嘗試使用選擇排序方法時,我的方法只能正確排序數字,直到它到達倒數第二個數字,然后它開始錯誤地切換數字。 誰能查看我的代碼並告訴我哪里出錯了?

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

}

這里是錯誤的 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]

在 3 塊中,您設置finalIndex = 0

它應該設置為 startIndex

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM