我正在尝试对“数据”进行排序。数据应该按数字排序。我正在尝试实现 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;
}
}
最佳答案
检查此代码是否有效
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());
swapDone=true;
}
current = current.next;
}
current = first;
}
}
关于java - 自定义链表上的冒泡排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23144625/