以下を使用して Dobly Linked List を実装しています 。割り当てのバブルソートを使用します。並べ替えとリンク リストについて調査した結果、インデックスを使用してリンク リストをバブル ソートするべきではないことがわかりました。リンク リストにはインデックスが存在しないか、うまく実装するのが面倒だからです。
それで、それを読んで、次のコードを書きましたが、正しい道にいるかどうかはまだわかりません。
ダブル リンク リストを使用したバブル ソートの実装の背後にあるロジックを理解するのに助けが必要です。
また、私が効率的に正しい道を進んでいるかどうか、またはこのコーディング演習の試みが完全に間違っているかどうかについて、ある程度の安心感が必要です。
//This for loop sorts the smaller part of the bubble sort.
for(int i = 0; i < cars.size() - 1; i++)
{ //This part creates the second "larger" part of the bubble sort.
for(int j = i + 1; j < cars.size(); j++)
{
//Did I do this part correctly? This is where the swap and sort of the bubble sort takes //place.
//Obviously, I am using the comparable interface, since I am using the compareTo method.
//
//with the bubblesort, all elements must be greater than zero because for the bubble //sort, 0 is the smallest element in a set of integers.
if(cars.get(i).getName().compareTo(cars.get(j).getName()) > 0)
{
CarName cari = cars.get(i);
CarName CarNamej = cars.get(j);
cars.remove(i);
cars.add(i, carj);
cars.remove(j);
cars.add(j, cari);
}
}
}
}
これを使用して、メイン メソッドでこのメソッドを出力し、並べ替えられた結果を出力します。
bubbleSort(cars);
私は正しいですか、それともすべてのコードで完全に間違ったことをしましたか?