繁体   English   中英

用通用方法实现的SelectionSort-错误的结果

[英]SelectionSort implemented with Generic Method - Wrong Results

我正在研究Deitel&Deitel的“ Java-如何编程”,并且为为什么我想到的这种通用选择排序方法的实现不起作用而感到困惑。 我确定我一定会遗漏一些小细节,但是在研究了API和有关泛型的一些资源之后,我感到很冷淡。 当程序运行并执行某种排序时,它绝对不会按数字顺序排序! 我不知道是误解泛型还是选择排序算法。 任何帮助,将不胜感激!

我在intArray上运行选择排序时收到的输出是:0、1,-23、7、54

排序后floatArray的输出为:-1.1,-10.3、0.4、4.5

更新我只是在不使用负值的情况下尝试了它,并且排序很好,这是怎么回事???

这是执行选择排序的完整Sorter类:

import java.util.Arrays;
import java.util.ArrayList;

public class Sorter {
    public static void main(String[] args) {
        Integer[] intArray = {1, 7, -23, 54, 0};
        Float[] floatArray = {0.4f, -10.3f, 4.5f, -1.1f};

        ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(intArray));
        ArrayList<Float> floatList = new ArrayList<>(Arrays.asList(floatArray));

        System.out.printf("Lists before selectionSort: %n%s%n%s%n%n",
            intList, floatList);

        selectionSort(intList);
        selectionSort(floatList);

        System.out.printf("Lists after selectionSort: %n%s%n%s%n%n",
            intList, floatList);
    }

    public static <T extends Comparable<T>> void selectionSort(ArrayList<T> list) {
        // helps determine whether or not a swap will occur
        boolean needsSorting = false;
        // keeps track of the index of the smallest value
        int smallest = 0;

        // outer for walks the portion of the list that will be swapped
        for (int i = 0; i < list.size() - 1; i++) {
            // inner for searches for a smaller value than the front of list
            for (int j = i + 1; j < list.size(); j++) {
                // if the inner value is less than the outer value
                if (list.get(j).compareTo(list.get(i)) < 0) {
                    // store the index of the smaller value
                    smallest = j;
                    // set the boolean flag to true so the sort will happen
                    needsSorting = true;
                }
            }

            // if the list needs sorting
            if (needsSorting) {
                // get the value of the outer loop, store in generic variable
                T temp = list.get(i);
                // replace value of outer loop with value at the smallest index
                list.set(i, list.get(smallest));
                // replace value at what was smallest index with the value that
                // was at the index of the outer loop
                list.set(smallest, temp);

                needsSorting = false;
            }
        }
    }
}

问题可能在此行出现,

if (list.get(j).compareTo(list.get(i)) < 0) 

代替list.get(i) ,它应该是list.get(smallest)

另外,当您在外部for循环的每次迭代中都这样做时,您并没有更新最小。 for (int i = 0; i < list.size() - 1; i++)之后立即添加此代码行

smallest = i;

DaneBrick为我设定了正确的方向。 对于外循环的每次迭代,我需要将最小变量设置为等于i,即外循环的计数器。 这是代码的更正部分:

    for (int i = 0; i < list.size() - 1; i++) {
        // inner for searches for a smaller value than the front of list
        //*** NEW PORTION, DIDN'T HAVE THIS BEFORE ***//
        smallest = i;
        for (int j = i + 1; j < list.size(); j++) {
            // if the inner value is less than the outer value
            if (list.get(j).compareTo(list.get(smallest)) < 0) {
                // store the index of the smaller value
                smallest = j;
                // set the boolean flag to true so the sort will happen
                needsSorting = true;
            }
        }

暂无
暂无

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

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