Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.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_Algorithm_Mergesort - Fatal编程技术网

Java 合并排序古怪

Java 合并排序古怪,java,algorithm,mergesort,Java,Algorithm,Mergesort,我一直在尝试实现我自己的MergeSort,它似乎适用于较小的值,但我正在以随机顺序在1-100000的数组上进行尝试,并在打印出来时混合一些奇怪的数字。我已经跟踪了10次了,但都没找到 public static void mergeSort(int[] array){ if(array.length > 1){ int midPoint = array.length/2; int[] leftArray = new int[midPoint];

我一直在尝试实现我自己的MergeSort,它似乎适用于较小的值,但我正在以随机顺序在1-100000的数组上进行尝试,并在打印出来时混合一些奇怪的数字。我已经跟踪了10次了,但都没找到

public static void mergeSort(int[] array){
    if(array.length > 1){

       int midPoint = array.length/2; 

       int[] leftArray = new int[midPoint];
       int[] rightArray = new int[array.length - midPoint];

       System.arraycopy(array, 0, leftArray, 0, midPoint);
       System.arraycopy(array, midPoint, rightArray, 0, array.length - midPoint);

       mergeSort(leftArray);
       mergeSort(rightArray);

       merge(leftArray, rightArray, array);
    }
}

public static void merge(int[] left, int[] right, int[] bigArray){
    int counterLeft = 0, counterRight = 0, counterNewArray = 0;

    while(counterLeft < left.length && counterRight < right.length){
        if(left[counterLeft] < right[counterRight]){
            bigArray[counterNewArray] = left[counterLeft];
            counterLeft++;
            counterNewArray++;
        }else{
            bigArray[counterNewArray] = right[counterRight];
            counterRight++;
            counterNewArray++;
        }
    }

        while(counterLeft < left.length){
                bigArray[counterNewArray] = left[counterLeft];
                counterLeft++;
        }

        while(counterRight < right.length){
                bigArray[counterNewArray] = right[counterRight];
                counterRight++;
        }

        if(bigArray.length < 500){
            System.out.println("Merged array:");
            for(int i = 0; i < bigArray.length; i++){
                System.out.println(bigArray[i]);
            }
        }
}
公共静态void合并排序(int[]数组){
如果(array.length>1){
int midPoint=array.length/2;
int[]leftArray=新int[中点];
int[]rightArray=新int[array.length-中点];
数组复制(数组,0,左数组,0,中点);
System.arraycopy(数组,中点,rightArray,0,array.length-中点);
合并排序(leftArray);
合并排序(rightArray);
合并(leftArray、rightArray、array);
}
}
公共静态无效合并(int[]左,int[]右,int[]bigArray){
int counterLeft=0,counterRight=0,counterNewArray=0;
while(counterLeft
合并
的末尾,当您添加每边剩余的内容时……您并不是在增加
counterNewArray
。这会导致一组值被分配到一个点,相互覆盖…并在
bigArray
的尾部留下无效值(零,IIRC)。

“一些奇怪的数字混合在一起”。。。。你能举个例子吗?编译器上的
int
类型有多大?有时溢出会和你玩把戏…@Floris:这是Java。整数的大小是32位。这是有道理的,我明白为什么我在跟踪中忽略了它。谢谢是的,这就解决了。