【问题标题】:Java: Bubble sort on linked list not working properlyJava:链表上的冒泡排序无法正常工作
【发布时间】:2020-02-09 23:53:11
【问题描述】:

我有一个冒泡排序,它适用于从文件中的数据创建的链表。如果数字小于我的起始头节点,我的排序算法将不起作用。如何更新我的代码来解决这个问题?

示例:如果我的列表是 3、1、8、5 它将打印 3 --> 5 --> 8 并且 1 无处可见

    public void bubbleSort(LinkedNode head) {

        LinkedNode previous; 
        LinkedNode current; 
        LinkedNode next; 

        boolean isSorted = true;

        //if list is empty or only 1 item is in list -> it is sorted
        if (head == null || head.getNext() == null) {
            return;
        }

        long start = System.currentTimeMillis(); //begin count for bubbleSort

        while(isSorted) {
            bubbleComparisons = 0;
            bubbleExchanges = 0;
            previous = null;
            current = head;
            next = head.getNext();
            isSorted = false;

               while(next != null) {
                    bubbleComparisons ++; //increment counter for each comparison made

                    if (current.getElement() > next.getElement()) {
                        if (head == current) {
                            head = next;
                        }
                        else {
                            previous.setNext(next);
                        }
                        current.setNext(next.getNext());
                        next.setNext(current);
                        isSorted = true;
                        current = next;
                        bubbleExchanges++;    

                    }
                    previous = current;
                    current = previous.getNext();
                    next = current.getNext();
            }

【问题讨论】:

    标签: java linked-list bubble-sort


    【解决方案1】:

    对代码的几次更新。我已经用 // Up 标记了它们。 我跑了几个测试用例,只是检查它是否解决了目的。

    public class Solution {
      static class LinkedNode {
    
        private int element;
        private LinkedNode next;
    
        LinkedNode(int element) {
          this.element = element;
        }
    
        public int getElement() {
          return element;
        }
    
        public void setElement(int element) {
          this.element = element;
        }
    
        public LinkedNode getNext() {
          return next;
        }
    
        public void setNext(LinkedNode next) {
          this.next = next;
        }
    
      }
    
      private int bubbleComparisons;
      private int bubbleExchanges;
    
      public LinkedNode bubbleSort(LinkedNode head) {
    
        LinkedNode previous;
        LinkedNode current;
        LinkedNode next;
    
        boolean isSorted = true;
    
        // if list is empty or only 1 item is in list -> it is sorted
        if (head == null) {
          return null;
        } else if (head.getNext() == null) {
          return head;
        }
    
        while (isSorted) {
    //      bubbleComparisons = 0; // Up - 08
    //      bubbleExchanges = 0;  // Up - 09
          previous = null;
          current = head;
          next = head.getNext();
          isSorted = false;
    
          while (next != null) {
            bubbleComparisons++; // increment counter for each comparison made
    
            if (current.getElement() > next.getElement()) {
              if (head == current) {
                head = next;
              } else {
                previous.setNext(next);
              }
              current.setNext(next.getNext());
              next.setNext(current);
              isSorted = true;
              previous = next; // Up - 01
              // current = next; // Up - 02
              next = current.next; // Up - 03
              bubbleExchanges++;
            } else { // Up - 06
              previous = current;
              current = current.getNext(); // Up - 10 not required
              next = current.getNext();
            }
          }
        }
        return head; // Up - 05
      }
    
      public static void main(String[] args) {
        LinkedNode head = new LinkedNode(10);
        head.setNext(new LinkedNode(8));
        head.getNext().setNext(new LinkedNode(7));
        head.getNext().getNext().setNext(new LinkedNode(5));
    
        LinkedNode current = head;
        while (current != null) {
          System.out.print(current.element + "->");
          current = current.next;
        }
        System.out.println();
        Solution s = new Solution();
        current = s.bubbleSort(head);
        while (current != null) {
          System.out.print(current.element + "->");
          current = current.next;
        }
        System.out.println();
        System.out.println("comp:" + s.bubbleComparisons);
        System.out.println("exh:" + s.bubbleExchanges);
      }
    }
    

    【讨论】:

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