【发布时间】:2014-04-17 22:37:11
【问题描述】:
我正在尝试对“数据”进行排序。假设数据按数字排序。我正在尝试实现 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 你已经在外循环中设置了
swapDone=false并且从未更新它。所以它会让外循环只运行一次。
标签: java linked-list customization bubble-sort