我正在尝试对“数据”进行排序。数据应该按数字排序。我正在尝试实现冒泡排序(),但无法处理相同的。
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;
}
}
答案 0 :(得分:1)
如果有效,请查看此代码
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;
}
}
答案 1 :(得分:0)
答案 2 :(得分:0)
这是工作准则。
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 = current.next.value;
current.next.value = temp;
swapDone = true;
}
current = current.next;
}
current = first;
}
}