简体   繁体   English

为什么我的选择排序程序没有为最后几个数字提供正确的 output?

[英]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 ).我一直在学习这个 MOOC.fi Java 课程,并遇到了关于在这个页面上开发选择排序算法的问题( 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.基本上,我完成了第 4 部分之前的所有步骤,但是当我尝试使用选择排序方法时,我的方法只能正确排序数字,直到它到达倒数第二个数字,然后它开始错误地切换数字。 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:这里是错误的 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 ;在 3 块中,您设置finalIndex = 0

It should be set to the startIndex它应该设置为 startIndex

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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