我想排序'數據'。數據假設按數字排序。我試圖實現BubbleSort(),但無法處理相同。BubbleSort自定義鏈接列表
public void bubbleSort() {
if (isEmpty())
{
System.out.println("The Empty List Is Already Sorted");
}
else if (first.next == null) {
System.out.println("One Element List Is Already Sorted");
}
else {
Node current = first;
boolean swapDone = true;
while (swapDone) {
swapDone = false;
while (current != null) {
if (current.next != null && current.value.getScore() > current.next.value.getScore()) {
Data temp = current.value;
current.value.setScore(current.next.value.getScore());
current.value.setName(current.next.value.getName());
current.next.value.setScore(temp.getScore());
current.next.value.setName(temp.getName());
}
current = current.next;
}
current = first;
}
}
@CyberneticTwerkGuruOrc我正在使用while,它是一樣的。 – user3337714
@ user3337714你已經在外循環內設置了'swapDone = false'並且從不更新它。所以它會讓外層循環只運行一次。 – maxx777