Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.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
Algorithm 以下合并算法的时间复杂度是多少?_Algorithm_Merge_Parallel Processing_Time Complexity - Fatal编程技术网

Algorithm 以下合并算法的时间复杂度是多少?

Algorithm 以下合并算法的时间复杂度是多少?,algorithm,merge,parallel-processing,time-complexity,Algorithm,Merge,Parallel Processing,Time Complexity,您将看到int[][]列表(排序的int数组的数组)。 你要合并所有这些。 时间复杂度是多少 我试着将数组分成几对,然后并行合并所有对 public static List<Integer> merge(int[][] array) throws InterruptedException { // number of array remaining to be merged int remaining = array.length;

您将看到
int[][]列表
(排序的int数组的数组)。 你要合并所有这些。 时间复杂度是多少

我试着将数组分成几对,然后并行合并所有对

public static List<Integer> merge(int[][] array) throws InterruptedException {
        // number of array remaining to be merged
        int remaining = array.length;

        while (remaining > 2) {
            List<Thread> threads = new LinkedList<>();
            for (int i = 0; i < remaining - 1; i += 2) {
                // DoMerge is a runnable that merges 
                // two array in O(n1 + n2) time (n1 and n2 are
                // lengths of the two given arrays)
                // DoMerge will also put the merged array
                // at position i in the given array
                Thread mergeThread = new Thread(new DoMerge(i, i + 1, array));
                threads.add(mergeThread);
                mergeThread.start();
            }
            //  wait for them all to finish
            for (Thread t : threads) {
                t.join();
            }
            // move the newly merged list to the front
            for (int j = 1, i = 2; i < remaining; i += 2, ++j) {
                array[j] = array[i];
                array[i] = null;
            }
            remaining = (int) Math.ceil(remaining / 2.0);
        }

        return combine(lists[0], lists[1]);
    }
公共静态列表合并(int[][]数组)引发InterruptedException{
//要合并的剩余阵列数
剩余整数=array.length;
而(剩余>2){
列表线程=新建LinkedList();
对于(int i=0;i<剩余-1;i+=2){
//DoMerge是一款融合了
//O(n1+n2)时间内的两个阵列(n1和n2为
//两个给定数组的长度)
//DoMerge还将放置合并的阵列
//在给定数组中的位置i处
线程合并线程=新线程(新的DoMerge(i,i+1,数组));
线程。添加(合并线程);
mergeThread.start();
}
//等待他们全部完成
用于(螺纹t:螺纹){
t、 join();
}
//将新合并的列表移到前面
对于(int j=1,i=2;i<剩余;i+=2,++j){
数组[j]=数组[i];
数组[i]=null;
}
剩余=(int)Math.ceil(剩余/2.0);
}
返回联合收割机(列表[0],列表[1]);
}
(假设处理器数量>=arrays.length)

我认为它的时间复杂度是log(n).k,其中k是要合并的每个数组的最大长度,n是数组的数量


这是正确的吗?

不幸的是,这是错误的

让我们假设一个“最佳情况”场景,其中k=n,并且所有初始数组都具有大小k,这将是可能的最佳分区。为了简单起见,我们还假设n是2的幂

由于CPU线程可用性的假设,每个迭代都是完全并行的,所以第一次迭代的时间复杂度将是O(k+k)(这与O(k)相同,但请稍候)

第二次迭代将在大小为2x的k阵列上“工作”,因此时间复杂度为O(2k+2k),下一次迭代将为O(4k+4k),直到最后一次迭代,其时间复杂度为O(n/2k+n/2k)考虑到最后合并最后两个部分并创建完整阵列的事实,这一点是意料之中的

让我们对所有迭代求和:*2k+4k+8k+…+nk=O(nk)


你不能低于nk,因为你最后必须创建一个完整的数组,所以k log(n)是不可能的。

你是说DoMerge是O(n1+n2)吗?您已经编写了O(n1*n2)。是的,“n1+n2”,对不起,如果您在a(可以并行运行无限多个线程,并且没有创建新线程的开销)上运行代码,那么O(log(n)k)是正确的。当然,在真正的机器上,一旦线程数大于物理CPU数,您将很快遇到瓶颈,之后我们将回到O(log(n)n)渐近时间复杂度。谢谢你的回答。我这里有点慢,你能解释一下为什么“2k+4k+8k+…+nk”等于knlog(n)吗?就我所见,几何级数的和是k(2+2^2+…+2^I)(假设n=2^I),这会产生k.nI平均值,我真的很困惑为什么它会变成k.nlog(n)而不仅仅是k。n@OneTwoThree-你说得对。。。我最初写这篇文章的时候已经太晚了,但现在答案已经更正了