简体   繁体   English

正确的Mergesort实施?

[英]Correct Mergesort implementation?

I was working on MergeSort and I wrote this code. 我正在研究MergeSort,并编写了这段代码。 The problem is, I'm not sure if my implementation is exactly the way that this algorithm is supposed to work. 问题是,我不确定我的实现是否正是该算法应该工作的方式。 According to what I have read, the list is broken down into halves until all the lists' size is 1, and then the sorted lists are simultaneously brought together. 根据我所读的内容,列表被分成两半,直到所有列表的大小为1,然后将排序后的列表同时放在一起。 For example, {2,4,1,5,8,7,6,3} should be broken down to {2,4,1,5} and {8,7,6,3} then these two should further be broken down simultaneously to {2,4},{1,5},{8,7} and {6,3}, and so on. 例如,应将{2,4,1,5,8,7,6,3}分解为{2,4,1,5}和{8,7,6,3},然后进一步将这两个同时分解为{2,4},{1,5},{8,7}和{6,3},依此类推。 However, with my code, the {2,4,1,5} is broken down to its components and then sorted completely before the other array ({8,7,6,3}) is even broken down. 但是,对于我的代码,{2,4,1,5}被分解成其组件,然后在另一个数组({8,7,6,3})被分解之前被完全排序。 I could not think of any way to work on both arrays simultaneously. 我想不出任何办法同时在两个阵列上工作。 My code seems to be working fine, but the question is: Is this code a valid implementation of MergeSort? 我的代码似乎运行良好,但问题是:该代码是否是MergeSort的有效实现?

public static void mergesort(int[] arr)
{
    if(arr.length == 1)
        return;
    int[] arr1 = new int[arr.length/2];
    int[] arr2 = new int[arr.length - arr1.length];
    int i = 0;
    for(int j = 0; j < arr1.length; j++, i++)
    {
        arr1[j] = arr[i];
    }
    for(int j = 0; j < arr2.length; j++, i++)
    {
        arr2[j] = arr[i];
    }
    mergesort(arr1);
    mergesort(arr2);
    int x = 0, y = 0, z = 0;
    while(x < arr1.length && y < arr2.length)
    {
        if(arr1[x] < arr2[y])
        {
            arr[z] = arr1[x];
            x++;
            z++;
        }
        else
        {
            arr[z] = arr2[y];
            y++;
            z++;
        }
    }
    if(x != arr1.length)
    {
        while(x < arr1.length)
        {
            arr[z] = arr1[x];
            x++;
            z++;
        }
    }
    else if(y != arr2.length)
    {
        while(y < arr2.length)
        {
            arr[z] = arr2[y];
            y++;
            z++;
        }
    }
}

public static void main(String[] args) {
    int[] arr = {2,4,1,5,8,7,6,3};
    mergesort(arr);
    for(int i = 0; i < arr.length; i++)
    {
        System.out.print(arr[i] + " ");
    }
}

Going off of your question and without having analyzed your code in-depth... 解决您的问题,而无需深入分析您的代码...

Basically, yes. 基本上是。 It is not necessary to work on both halves simultaneously. 不必同时在两个半部上工作。 In fact, you can completely sort one half before you even look at the second half. 实际上,您甚至可以在不看第二部分的情况下就完全将其分类。 That doesn't hurt the correctness of the algorithm. 这不会损害算法的正确性。

In a slightly different vein, if you want to make it more efficient (ie sort each independent half at the same time), then you would have to use multithreading, which is much more difficult to code by hand. 以稍微不同的方式,如果要提高效率 (即同时对每个独立的半部分进行排序),则必须使用多线程,手工编写起来要困难得多。

(and if this is for a fundamental algorithms course, you probably don't need to know how to make a multi-threaded MergeSort anyway, since that would involve a number of design decisions relating to concurrency / synchronization / thread pools, which is a whole different topic) (如果这是一门基础算法课程,那么您可能根本不需要知道如何制作多线程MergeSort,因为这将涉及与并发/同步/线程池有关的许多设计决策,这是一个整个不同的主题)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM