java - 自定义链表上的冒泡排序

标签 java linked-list customization bubble-sort

我正在尝试对“数据”进行排序。数据应该按数字排序。我正在尝试实现 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/

相关文章:

c - C 中的链表,大数的加法。分段故障

java - contains方法如何匹配?

user-interface - 有什么好的方法可以处理UI中的大量排列?

javascript - 如何继承Odoo POS按钮

java - 为什么 Java 允许将原始 List 传递给通用 List 构造函数?

java - Findbugs & Ant - 停止特定违规的构建

java - Android 从类中读/写

java - 安卓/Java : How to use Tor-Onion-Proxy-Library?

c - 基本C链表语法错误

angular - 为 Angular 2 中的每个用户自定义 URL