簡體   English   中英

如何在Java中使用比較器進行選擇排序?

[英]How to use selection sort with comparator in java?

這里的幫助將不勝感激。 我很困。 我需要做的是我必須使用選擇排序算法對一個數組列表進行排序,但是數組列表中的每個對象都有多個索引。 這必須在Java中完成。

例如:

public class a {

    private int number;
    private String letter;

    public a(int n, String l)
    {
        number = n;
        letter = l;
    }
}

public class SortingArrays {

    private ArrayList<a> myarray;

    private Comparator<a> sortByNumber;
    private Comparator<a> sortByLetter;

    public FootballPlayerData() {

        myarray = new ArrayList<a>();

        getmyarray().add(new a(2, "A"));
        getmyarray().add(new a(7, "J"));

        //COMPARATORs//
        sortByNumber = new Comparator<a>() {
            @Override
            public int compare(a o1, a o2) 
            {
                if (o1.number < (o2.number)) {
                    return -1;
                }
                if (o1.number == (o2.number)) {
                    return 0;
                }
                return 1;
            }

        };
        sortByLetter = new Comparator<a>() {
            @Override
            public int compare(a o1, a o2) 
            {
                return o1.letter.compareTo(o2.letter);
            }

         };

    public void selectionSortbyNumber
    {
        ???
    }
    public void selectionSortbyLetter
    {
        ???
    }
}

那么,如何在Java中創建選擇排序(必須是選擇排序),從而根據對象內的不同元素對arraylist進行排序? 我已經把比較器部分放下了,但是我不知道如何將它與選擇排序結合在一起。

來自https://en.wikipedia.org/wiki/Selection_sort的來源:

/* a[0] to a[n-1] is the array to sort */
int i,j;
int n;

/* advance the position through the entire array */
/*   (could do j < n-1 because single element is also min element) */
int iMin;
for (j = 0; j < n-1; j++) {
    /* find the min element in the unsorted a[j .. n-1] */
    /* assume the min is the first element */
    iMin = j;
    /* test against elements after j to find the smallest */
    for (i = j+1; i < n; i++) {
        /* if this element is less, then it is the new minimum */
        if (a[i] < a[iMin]) {
            /* found new minimum; remember its index */
            iMin = i;
        }
    }
}

if (iMin != j) {
    swap(a[j], a[iMin]);
}

方法swap()只是切換數組中的值。

您的工作將是用列表交換數組。 :P但這並不難,因為您可以通過索引get(int index)方法訪問列表值。

Comparator實現通常用於比較兩個元素,如果第一個元素小於第二個,則返回-1 (或任何負數);如果相等,則返回0如果第一個元素小於1,則返回1 (或任何正數)。元素大於第二個元素。 這可用於比較兩個元素,以查看一個元素是否大於,小於或等於另一個。

在選擇排序的上下文中,可以使用提供的比較器來確定列表中未排序部分中的哪個值是最小值。 選擇排序的一般算法如下:

for i from 0 to array.length:
    current_minimum_index = i
    for j from i + 1 to array.length:
        if array at j is less than array at current_minimum_index:
            current_minimum_index = j

        swap array at current_minimum_index with array at i

可以使用Comparator實現if array at j is less than array at current_minimum_indexif array at j is less than array at current_minimum_index 例如,給定提供的名為array ArrayList ,對名為comparatorComparator對象的調用將為:

if (comparator.compare(array.get(j), array.get(current_minimum_index))) < 0)

我不想為您提供完整的答案,因為這不會幫助您學習選擇排序,但是排序的方法簽名類似於以下內容:

public <T> void selectionSort(ArrayList<T> array, Comparator<T> comparator) {

    // for i from 0 to array.size():
        // currentMinIndex = i
        // for j from i + 1 to array.size():
            if (comparator.compare(array.get(j), array.get(currentMinIndex))) < 0) {
                // currentMinIndex = j
            }

            // swap array at currentMinIndex with array at i
}

您對此方法的調用將類似於以下之一:

selectionSort(myarray, sortByNumber);
selectionSort(myarray, sortByLetter);

暫無
暫無

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

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