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

时间:2016-04-06 00:31:59

标签: java sorting generics

我正在通过Deitel& amp; 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;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

问题可能出在这一行,

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;

答案 1 :(得分:0)

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