Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的合并排序实现有什么问题?_Java_Sorting_Merge_Mergesort - Fatal编程技术网

Java 我的合并排序实现有什么问题?

Java 我的合并排序实现有什么问题?,java,sorting,merge,mergesort,Java,Sorting,Merge,Mergesort,这是我对带有合并排序的列表的实现: public class List { private final Object data; private final List next; public static final List NIL = new List(null, null); private List(Object d, List n) { data = d;

这是我对带有合并排序的列表的实现:

public class List {

        private final Object data;
        private final List next;
        public static final List NIL = new List(null, null);

        private List(Object d, List n) {
                data = d;
                next = n;
        }

        public static List list(Object d) {
                return new List(d, List.NIL);
        }

        public List append(List that) {
                return this.isEmpty() ?
                        that :
                        this.tail().append(that).push(this.head());
        }

        public Object nth(int n) {
                return this.isEmpty() || n < 0 ? null :
                                n == 0 ? this.head() :
                                this.tail().nth(n-1);
        }

        public List sort() {
                return mergesort(0, this.length());
        }

        private List mergesort(int top, int out) {
                List l = null;
                if (top < out - 1) {
                        int mid = (top + out) / 2;
                        mergesort(top, mid);
                        mergesort(mid, out);
                        l = merge(top, mid, out);
                }
                return l;
        }

        private List merge(int top1, int top2, int out) {
                List temp = List.NIL;
                List toReturn = this;
                int i = top1;
                int j = top2;
                int k = 0;

                while (i < top2 && j < out) {
                        Comparable one = (Comparable) this.nth(i);
                        Comparable two = (Comparable) this.nth(j);
                        if (one.compareTo(two) < 0) {
                                temp = temp.append(List.list(this.nth(i++)));
                        } else {
                                temp = temp.append(List.list(this.nth(j++)));
                        }
                }

                while (i < top2) temp = temp.append(List.list(this.nth(i++)));
                while (j < out) temp = temp.append(List.list(this.nth(j++)));

                for (k = 0; k < temp.length(); k++)
                        toReturn = toReturn.append(List.list(temp.nth(k)));

                return toReturn;

        }

}
公共类列表{
私有最终对象数据;
接下来是私人最终名单;
公共静态最终列表NIL=新列表(null,null);
私有列表(对象d,列表n){
数据=d;
next=n;
}
公共静态列表(对象d){
返回新列表(d,List.NIL);
}
公共列表附加(列出该列表){
返回这个。isEmpty()?
即:
this.tail().append(that.push)(this.head());
}
公共对象n(整数n){
返回此.isEmpty()| | n<0?null:
n==0?this.head():
this.tail().nth(n-1);
}
公共列表排序(){
返回mergesort(0,this.length());
}
私有列表合并排序(int-top,int-out){
列表l=空;
如果(顶部<外部-1){
int mid=(顶部+外部)/2;
合并排序(顶部、中部);
合并排序(中间、外部);
l=合并(顶部、中部、外部);
}
返回l;
}
私有列表合并(int top1、int top2、int out){
列表温度=List.NIL;
列表返回=此;
int i=top1;
int j=top2;
int k=0;
而(i
这段代码似乎正确地实现了合并排序,但排序时我得到了错误的结果
654321
:我得到
321654
,我不知道为什么。我不确定这是否只是我还没有看到的一件小事,或者这是一个根本性的问题