Java 为什么这种排序算法只对最终整数的值进行排序?

Java 为什么这种排序算法只对最终整数的值进行排序?,java,algorithm,sorting,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,

每当我尝试对这个链表排序时,它只会将其排序到列表中的最后一个数字。例如,对于[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());
    }
}
class排序表
{
{
私钥;
私有节点下一步;
专用节点(int键,下一个节点)
{
this.key=key;
this.next=next;
}
}
专用节点头;
私有节点优先;
公众乘客名单()
{
head=新节点(0,空);
}
公共航班表(国际航班优先,国际航班…剩余)
{
节点最后一个=新节点(第一个,空);
这个。第一个=最后一个;
对于(int index=0;indexright.key){
合并=右;
right=right.next;
}否则{
合并=左;
left=left.next;
}
结束=合并;
while(左!=null和右!=null){
if(left.key>right.key){
end.next=右;
right=right.next;
}否则{
end.next=左;
left=left.next;
}
end=end.next;
}
if(左!=null){
结束=左;
}else if(右!=null){
结束=右;
}
返回合并;
}
公共字符串toString()
{
StringBuilder=新的StringBuilder();
builder.append('[');
如果(第一个!=null)
{
节点温度=第一;
生成器追加(临时键);
温度=下一个温度;
while(temp!=null)
{
生成器。追加(“,”);
生成器追加(临时键);
温度=下一个温度;
}
}
builder.append(']');
返回builder.toString();
}
公共静态void main(字符串[]args)
{
println(新的排序列表(5,8,4,9,0,1,2,3,7,6).sort());
}
}

我没有研究过你的算法,但你的代码似乎有几个问题,例如

  • 节点
    没有类名

  • 您使用了
    head
    节点,但它在代码中似乎没有用处

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

    正如Eran提到的,这种情况是不正确的。如果删除最终条件,程序将在运行时执行无休止的递归:

    left=排序(lo)

    这就是为什么会出现堆栈溢出异常

  • 节点端

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

    //它什么也不做
    if(左!=null){
    结束=左;
    }else if(右!=null){
    结束=右;
    }


  • 这不是唯一的问题,但是条件
    if(unsorted==null | | | unsorted.next==null | | unsorted.next.next==null){..}
    意味着您没有对两个元素的列表进行排序。当我删除最后一个条件时,会出现堆栈溢出错误。您尝试过调试吗?