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