【问题标题】:BubbleSort on Custom LinkedList自定义链表上的冒泡排序
【发布时间】: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;
        }
    }

【问题讨论】:

  • @Cyber​​neticTwerkGuruOrc 我在用while,是一样的。
  • @user3337714 你已经在外循环中设置了swapDone=false 并且从未更新它。所以它会让外循环只运行一次。

标签: java linked-list customization bubble-sort


【解决方案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;
        }
    }

【讨论】:

  • 如果我传递两个数字 Obj1: 180 Obj2: 60 它会打印 Obj2: 20 Obj2: 20 上面的代码。
  • 打印语句在哪里?我还没有提供你的代码
  • 打印语句以另一种方法运行。这是我的代码的方法之一。我真的无法弄清楚出了什么问题。
  • 我认为这种方法除了优化之外没有任何问题。检查您是否在其他地方修改变量。
  • 我没有在其他任何地方进行修改。这只是处理变量的方法。 Print 方法只是打印 LinkedList。 @弗雷泽价格
【解决方案2】:

swapDone 在第一个循环后总是为假。你只会经历一次内循环

【讨论】:

  • 所以我想在交换之后更新'if'语句中的swapDone,即swapDone = true; @maxx777 和弗雷泽价格
  • 您需要在内部循环结束时更新 swapDone。如果该算法的通过没有任何变化,则这应该是错误的(否则保持正确)。
  • 另外,swapDone 这个名字有误导性。如果此值为 false 而不是 true,则主循环将退出。
  • 这就是我对你的代码所做的。检查它是否有效。 @fraser 提供的建议也很好。也试试那个。
【解决方案3】:

这是工作代码。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 2012-09-29
    • 2013-04-08
    相关资源
    最近更新 更多