为什么这种排序算法只排序到最终整数的值?

时间:2017-04-24 06:12:34

标签: java algorithm sorting

每当我尝试对此链接列表进行排序时,它只会将其排序到列表中的最终数字。因此,例如,使用[5,8,4,9,0,1,2,3,7,6]的链表,唯一的回报是[0,1,2,3,4,5,6]。我觉得这里有一个愚蠢的错误,尽管最后一小时试图找到它,但我还没能识别出来。

这是我的代码:

class SortyList
{
    {
        private int  key;   
        private Node next; 

        private Node(int key, Node next)
        {
            this.key  = key;
            this.next = next;
        }
    }

    private Node head;  
    private Node first;  

    public SortyList()
    {
        head = new Node(0, null);
    }

    public SortyList(int first, int ... rest)
    {
        Node last = new Node(first, null);
        this.first = last;
        for (int index = 0; index < rest.length; index += 1)
        {
            last.next = new Node(rest[index], null);
            last = last.next;
        }
        head = new Node(0, null);
    }

    public SortyList sort()
    {
        first = sort(first);
        return this;
    }

    private Node sort(Node unsorted)
    {

       if (unsorted == null || unsorted.next == null || unsorted.next.next == null) {
           return unsorted;
       }
       Node left = unsorted;
       Node lo = left;
       unsorted = unsorted.next;
        Node right = unsorted;
        Node ro = right;
        unsorted = unsorted.next;
       for (int i = 0; unsorted != null; i++) {
           if (i % 2 == 0) {
               Node temp = left;
               left = unsorted;
               temp.next = left;
           } else {
               Node temp = right;
               right = unsorted;
               temp.next = right;
           }
           unsorted = unsorted.next;
       }
       Node r = lo;

       left = sort(lo);
       right = sort(ro);
       Node merged;
       Node end;
        if (left.key > right.key) {
            merged = right;
            right = right.next;
        } else {
            merged = left;
            left = left.next;
        }
        end = merged;
       while (left != null && right != null) {
           if (left.key > right.key) {
               end.next = right;
               right = right.next;
           } else {
               end.next = left;
               left = left.next;
           }
           end = end.next;
       }

       if (left != null) {
           end = left;

       } else if (right != null) {
           end = right;
       }

       return merged;
    }

    public String toString()
    {
        StringBuilder builder = new StringBuilder();
        builder.append('[');
        if (first != null)
        {
            Node temp = first;
            builder.append(temp.key);
            temp = temp.next;
            while (temp != null)
            {
                builder.append(", ");
                builder.append(temp.key);
                temp = temp.next;
            }
        }
        builder.append(']');
        return builder.toString();
    }

    public static void main(String[] args)
    {
        System.out.println(new SortyList(5, 8, 4, 9, 0, 1, 2, 3, 7, 6).sort());
    }
}

1 个答案:

答案 0 :(得分:0)

我没有查看过您的算法,但似乎您的代码存在一些问题,例如

  1. Node没有班级名称。

  2. 您使用的是head节点,但它似乎对您的代码没用。

  3. if (unsorted == null || unsorted.next == null || unsorted.next.next == null)

    正如伊兰所说,这种情况是不正确的。如果删除最终条件,程序将在运行时进行无休止的递归:

    left = sort(lo);

    这就是为什么你有一个堆栈溢出异常。

  4. Node end;

    end是一个局部变量,它在sort(Node unsorted)函数的最后几行中没有为它赋值:

    // it does nothing if (left != null) { end = left; } else if (right != null) { end = right; }

  5. ...