簡體   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