0

バブルソートを使用してアイテムのリストをソートしようとしています。これが最も効果的な並べ替え方法ではないことは承知しています。ただし、より良いものに移る前に、この方法が機能する必要があります。私の現在のコードはリストを部分的にソートしていますが、何が間違っているのかわかりません。

public class ListComponents
{
    public int Priority;
    public string Name;
    public ListComponents Next;
}

public void bubblesort()
{
    ListComponents Current = Head; 
    int temp = 0;
    bool swapped = true;

    while (swapped)
    {
        Print(); // Debug Function to print each swap
        Current = Head.Next; // RESET CURRENT TO HEAD + 1 ON EACH ITERATION?
        swapped = false;
        for (int sort = 0; sort < (size - 1); sort++) //Size defined as # of items in list
        {
            if (Current != null && 
                Current.Next != null && 
                Current.Priority> Current.Next.Priority)
            {
                temp = Current.Priority;
                Current.Priority= Current.Next.Priority;
                Current.Next.Priority= temp;
                Current = Head; // POTENTIAL ISSUE?
                swapped = true;
            }
        }
    }
}

私のデバッグ出力関数は次のように表示され、値がほぼ順番に並べられていることを示しています。

List: 4 2 1 3  (Inital List Order)
List: 1 4 2 3  (First Swap)
List: 1 2 4 3  (Second Swap)

これが機能していない場所はわかりませんが、問題は「現在」の値の設定にあるようです。

4

2 に答える 2

13

私のアドバイス:最初からやり直してください。これは混乱です。

私が初心者だったときにこのような問題に取り組む方法は次のとおりです。疑似コードでコードを書くことから始めます。

void BubbleSort(ListComponent head)
{
    if the list is of zero length then it is already sorted, so return
    if the list is of length one then it is already sorted, so return
    Otherwise, the list is of length two or more. Therefore we know that
    a first item exists and a second-last item exists.
    Our strategy is: run down the list starting with the first item
    and ending with the second-last item. If at any time the current
    item and the one following it are out of order, swap their elements.
    If we made no swaps then the list is sorted, return. 
    If we made one or more swaps then the list might not be sorted, so 
    run down the list again.
}

OK、これでコードへの変換を開始できます。

void BubbleSort(ListComponent head)
{
    // if the list is of zero length then it is already sorted, so return
    if (head == null) return; 

    // if the list is of length one then it is already sorted, so return
    if (head.Next == null) return; 

    // Otherwise, the list is of length two or more. Therefore we know that
    // a first item exists and a second-last item exists.

    // Our strategy is: run down the list starting with the first item
    // and ending with the second-last item. 

    for (ListComponent current = head; 
        current.Next != null; 
        current = current.Next)
    {

        If at any time the current item and the one following it
        are out of order, swap their elements.
    }

    If we made no swaps then the list is sorted, return. 
    If we made one or more swaps then the list might not be sorted, so 
    run down the list again.
}

OK、英語の一部をコードに翻訳しました。残りを翻訳していただけますか?

于 2013-03-25T21:54:25.767 に答える