Java 递归合并排序结果与原始排序结果相同

Java 递归合并排序结果与原始排序结果相同,java,Java,我正在做一个合并排序,它排序和int[]。我的mergeSort方法接受数组、startindex和endindex。我还在main方法中输出before和after。after数组的结果与before相同。我不知道我的alg有什么问题。我一直在查阅其他人对合并排序的看法,看起来我做得不错。显然不是,因为列表没有排序。。我的合并排序算法有什么错误 更新:所以我对我的代码做了一些修改,看起来分割过程很好,但是合并代码时不会合并排序列表。取而代之的是,它只是使用最初划分为两部分的内容进行排序。这可以

我正在做一个合并排序,它排序和int[]。我的mergeSort方法接受数组、startindex和endindex。我还在main方法中输出before和after。after数组的结果与before相同。我不知道我的alg有什么问题。我一直在查阅其他人对合并排序的看法,看起来我做得不错。显然不是,因为列表没有排序。。我的合并排序算法有什么错误

更新:所以我对我的代码做了一些修改,看起来分割过程很好,但是合并代码时不会合并排序列表。取而代之的是,它只是使用最初划分为两部分的内容进行排序。这可以在合并的最后一步中看到。合并的两个数组应该进行排序,这样,当您逐个比较每个索引时,两个索引中较小的一个应该按正确的顺序放入新列表中

正在合并的两个列表是{2 3 2 52 64 2}&{1 8 54 8 32 7},这与原来的{2 3 2 52 64 2 2 1 8 54 8 32 7}刚刚划分为2的列表相同

新代码:

public static void main(String [] args) {
    int [] array = {2,3,2,52,64,2,1,8,54,8,32,7};

    int[] newarray = mergeSort(array, 0 ,array.length);


    System.out.println("\n");
    System.out.println("Unsorted List:");
    for (int i = 0; i < array.length; i++) {
        System.out.print(array[i] + " ");
    }

    System.out.println("\n");
    System.out.println("Sorted List:");
    for (int i = 0; i < newarray.length; i++) {
        System.out.print(newarray[i] + " ");
    }
}


public static int[] mergeSort(int[] list, int start, int end) {
    if ((end-start) <= 1) {
        return list;
    }

        int mid = (start+end)/2;

        mergeSort(list, start, mid);
        mergeSort(list, mid, end);
        return merge(list, start, mid, end);
        //return list;
    }

public static int[] merge(int[]list, int start, int mid, int end) {
    int[] tempList = new int[list.length];

    System.out.println("First half");
    for (int i = start; i < mid; i++) {
        System.out.print(list[i] + " ");
    }
    System.out.println();
    System.out.println("Second half");
    for (int i = mid; i < end; i++) {
        System.out.print(list[i] + " ");
    }
    System.out.println();

    int i=start;
    int j=mid;
    int k=0;


    while (i < mid && j < end) {
        if (list[i] <= list[j]) {
            tempList[k] = list[i];
            i++;
            k++;
        } else {
            tempList[k] = list[j];
            j++;
            k++;
        }
    }

    while (i < mid) {
        tempList[k] = list[i];
        i++;
        k++;
    }
    while (j < end) {
        tempList[k] = list[j];
        j++;
        k++;
    }

    System.out.println("Merged");
    for (int z= 0 ; z < tempList.length; z++) {
        System.out.print(tempList[z] + " ");
    }
    System.out.println("\n");

    return tempList;
}
publicstaticvoidmain(字符串[]args){
int[]数组={2,3,2,52,64,2,1,8,54,8,32,7};
int[]newarray=mergeSort(数组,0,array.length);
System.out.println(“\n”);
System.out.println(“未排序列表:”);
for(int i=0;i
1.概述

Mergesort算法可用于对一组对象进行排序。Mergesort是一种所谓的分治算法。分治算法将原始数据分割成较小的数据集来解决该问题

2.合并排序

在Mergesort过程中,集合的对象分为两个集合。要拆分集合,Mergesort将占据集合的中间部分,并将集合拆分为左侧和右侧

生成的集合再次通过Mergesort算法进行递归排序

两个集合的排序过程完成后,将合并两个集合的结果。要合并两个集合,Mergesort从每个集合的开始处开始。它选择较小的对象并将此对象插入新集合。对于此集合,它现在选择下一个元素并选择两个集合中的maller元素

将两个集合中的所有元素插入新集合后,Mergesort将成功对集合进行排序

为了避免创建过多集合,通常会创建一个新集合,并将左侧和右侧视为不同的集合

3.实施

创建以下程序

public class Mergesort {
    private int[] numbers;
    private int[] helper;

    private int number;

    public void sort(int[] values) {
            this.numbers = values;
            number = values.length;
            this.helper = new int[number];
            mergesort(0, number - 1);
    }

    private void mergesort(int low, int high) {
            // check if low is smaller then high, if not then the array is sorted
            if (low < high) {
                    // Get the index of the element which is in the middle
                    int middle = low + (high - low) / 2;
                    // Sort the left side of the array
                    mergesort(low, middle);
                    // Sort the right side of the array
                    mergesort(middle + 1, high);
                    // Combine them both
                    merge(low, middle, high);
            }
    }

    private void merge(int low, int middle, int high) {

            // Copy both parts into the helper array
            for (int i = low; i <= high; i++) {
                    helper[i] = numbers[i];
            }

            int i = low;
            int j = middle + 1;
            int k = low;
            // Copy the smallest values from either the left or the right side back
            // to the original array
            while (i <= middle && j <= high) {
                    if (helper[i] <= helper[j]) {
                            numbers[k] = helper[i];
                            i++;
                    } else {
                            numbers[k] = helper[j];
                            j++;
                    }
                    k++;
            }
            // Copy the rest of the left side of the array into the target array
            while (i <= middle) {
                    numbers[k] = helper[i];
                    k++;
                    i++;
            }

    }
}
公共类合并排序{
私有int[]编号;
私人助理;
私有整数;
公共void排序(int[]值){
这个数字=数值;
数字=值。长度;
this.helper=newint[number];
合并排序(0,数字-1);
}
私有void合并排序(整数低,整数高){
//检查low是否比high小,如果不是,则对数组进行排序
如果(低<高){
/获取中间元素的索引
int middle=低+(高-低)/2;
//对数组的左侧排序
合并排序(低、中);
//对数组的右侧进行排序
合并排序(中+1,高);
//把两者结合起来
合并(低、中、高);
}
}
私有无效合并(整数低、整数中、整数高){
//将这两个部分复制到辅助对象数组中

对于(int i=low;i),您发布的代码将无法编译。请在发布前进行测试。在merge()的末尾,而不是返回templast[],将templast[]复制回list[]。@rcgldr是否应将返回列表[]保留在mergeSort()中?最好由用户调用一个单独的输入函数。此输入函数将一次性分配templist,并将其作为参数传递给mergeSort()和merge()。完成排序后,entry函数将释放templist。通过此实现,您可以通过在mergeSort()中使用布尔参数或通过使用mergeSort()的两个实例,使合并方向随递归级别而更改,从而消除复制操作,一个是数据在列表中结束,一个是数据在templist中结束。如果有兴趣,我可以稍后解释。这些都不能解释OP为什么会看到他的输出。