自定义链接列表上的冒泡排序

时间:2014-04-17 22:37:12

标签: java linked-list customization bubble-sort

我正在尝试对“数据”进行排序。数据应该按数字排序。我正在尝试实现冒泡排序(),但无法处理相同的。

    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;
        }
    }

3 个答案:

答案 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)

第一次循环后,
swapDone将始终为false。你只会经历一次内循环

答案 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;
    }
}