【问题标题】:Bubble sort algorithm for a linked list链表的冒泡排序算法
【发布时间】:2012-02-17 09:41:45
【问题描述】:

我编写了一个冒泡排序算法来对链表进行排序。我是一名 Java 初学者,正在尝试学习数据结构。我很困惑为什么我的第二个元素没有正确排序。

class SListNode {
    Object item;
    SListNode next;

    SListNode(Object obj) {
        item = obj;
        next = null;
    }

    SListNode(Object obj, SListNode next) {
        item = obj;
        this.next = next;
    }
}
public class SList {
    private SListNode head;
    private SListNode temp;

    public void sortList() {
        SListNode node = head, i, j;
        head = node;
        i = node;
        j = node.next;
        while (i.next != null) {
            while (j.next != null) {
                if ((Integer) i.item < (Integer) j.item) {
                    temp = i.next;
                    i.next = j.next;
                    j.next = temp;
                }
                j = j.next;
            }
            i = i.next;
        }
    }
}

这是我得到的输出

List after construction: [  3  6  9  4  12  15  ]
After sorting: [  3  4  9  12  6  15  ]

此外,我知道冒泡排序的最坏情况是 O(n2)。我可以在链表上使用归并排序来获得更好的时间复杂度吗?

【问题讨论】:

    标签: java sorting linked-list mergesort bubble-sort


    【解决方案1】:

    有许多排序算法适用于链表,合并排序在这种情况下效果很好。我写的 an earlier answer to a question about sorting linked lists 探讨了链表上的许多经典排序算法,以及它们的时间和空间复杂性。您可以对链表使用插入排序、选择排序、归并排序和快速排序。稍加修改,你也可以让 heapsort 工作。我较旧的答案详细说明了如何执行此操作。

    关于您的代码,请注意,在您的内部循环中,您将j 向前推进,直到next 指针变为null。此时,您永远不会将j 重置为其他任何东西,因此在外部循环的每次未来迭代中,内部循环都不会执行。您可能应该在每次迭代开始时设置j = i.next。此外,您可能不希望在j.next 为空时停止循环,而是在j 为空时停止循环,否则会跳过数组的最后一个元素。

    此外,您在此处编写的排序算法是 selection sort 而不是冒泡排序,因为您在链表上进行了多次遍历,以寻找您没有找到的最小元素定位呢。我不知道这是否是一个问题,但我不确定你是否意识到这一点。也就是说,我认为这可能是一件好事,因为在大多数情况下,冒泡排序的效率低于选择排序(除非列表已经接近排序)。

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      如果您有ListComparable 项,那么您可以使用Collections.sort 方法,该方法使用一种合并排序 算法,最坏情况时间复杂度为O(n log n)。无论如何,它比使用O(n²) 冒泡排序更快。

      如果你想使用冒泡排序算法,你可以这样做:

      public static void bubbleSort(List<Integer> list) {
          boolean swapped;
          // repeat the passes through the list until
          // all the elements are in the correct order
          do {
              swapped = false;
              // pass through the list and
              // compare adjacent elements
              for (int i = 0; i < list.size() - 1; i++) {
                  // if this element is greater than
                  // the next one, then swap them
                  if (list.get(i) > list.get(i + 1)) {
                      Collections.swap(list, i, i + 1);
                      swapped = true;
                  }
              }
              // if there are no swapped elements at the
              // current pass, then this is the last pass
          } while (swapped);
      }
      
      public static void main(String[] args) {
          LinkedList<Integer> list = new LinkedList<>();
          Collections.addAll(list, 6, 1, 5, 8, 4, 3, 9, 2, 0, 7);
          bubbleSort(list);
          System.out.println(list); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
      }
      

      另见:
      Bubble sort output is incorrect
      Bubble sort with step-by-step output Java 8

      【讨论】:

        猜你喜欢
        • 2011-09-04
        • 1970-01-01
        • 2012-09-29
        • 2016-03-19
        • 1970-01-01
        • 1970-01-01
        • 2012-07-19
        相关资源
        最近更新 更多