简体   繁体   English

将两个已排序的 arrays 合并为更大的排序数组的时间复杂度是多少?

[英]What is the time complexity of merging two sorted arrays into a larger sorted array?

The code below is my solution to the following problem:下面的代码是我对以下问题的解决方案:

Given 2 arrays of integers sorted in ascending order, write a function that merges those two arrays into one larger sorted array.给定 2 个 arrays 按升序排序的整数,编写一个 function 将这两个 arrays 合并为一个更大的排序数组。 For example, given the arrays arr1 = [[0, 3, 4, 31] and arr2 = [4, 6, 30] , the solution function should return the array [0, 3, 4, 4, 6, 30, 31] .例如,给定 arrays arr1 = [[0, 3, 4, 31]arr2 = [4, 6, 30] ,解决方案 function 应该返回数组[0, 3, 4, 4, 6, 30, 31] .

My solution:我的解决方案:

function mergeSortedArrays(arr1, arr2) {
    // Check input
    if (arr1.length === 0 && arr2.length === 0) {
        return arr1;
    } else if (arr1.length === 0) {
        return arr2;
    } else if (arr2.length === 0) {
        return arr1;
    }

    // Initialize variables
    let i = 0, j = 0;
    const mergedArray = [];

    // Loop through & compare
    while (i < arr1.length && j < arr2.length) {
        if (arr1[i] <= arr2[j]) {
            mergedArray.push(arr1[i]);
            i++;
        } else {
            mergedArray.push(arr2[j]);
            j++;
        }
    }

    if (j < arr2.length) {
        while (j < arr2.length) {
            mergedArray.push(arr2[j]);
            j++;
        }
    } else if (i < arr1.length) {
        while (i < arr1.length) {
            mergedArray.push(arr1[i]);
            i++;
        }
    }

    return mergedArray; // O(1)
}

The solution above works fine.上面的解决方案工作正常。 However, I'm having some trouble analyzing the worst-case time complexity of the algorithm I've used.但是,我在分析我使用的算法的最坏情况时间复杂度时遇到了一些麻烦。 At first glance, it appears to have linear time complexity, O(n), but upon further inspection, the number of iterations in the first loop that compares elements between the two arrays is not exactly bound simply by the length of either of the two arrays.乍一看,它似乎具有线性时间复杂度,O(n),但经过进一步检查,比较两个 arrays 之间元素的第一个循环中的迭代次数并不完全受限于两者中任何一个的长度arrays。 More specifically, it appears to be related to the upper and lower bounds of each array.更具体地说,它似乎与每个数组的上限和下限有关。

TLDR: What is the time complexity of the function above? TLDR:上面的 function 的时间复杂度是多少?

O(m+n) where m and n are size of respective arrays. O(m+n) 其中 m 和 n 分别是 arrays 的大小。

Time complexity for this algorithm will be O(arr1.length + arr2.length) since effectively we are iterating both the arrays once.该算法的时间复杂度将是 O(arr1.length + arr2.length),因为我们实际上是对 arrays 进行了一次迭代。

inside the first while loop, on every iteration, you will have either i or j being incremented.在第一个 while 循环中,在每次迭代中,都会增加 i 或 j 。 The maximum amount of times the while loop can be run is (arr.length - 1) + (arr2.length - 1), as any more will break out of the testing conditions. while 循环可以运行的最大次数是 (arr.length - 1) + (arr2.length - 1),因为再多的次数就会超出测试条件。

Lets call the final state of i and j here as k and l, where either k or l will be equal to their respective upperbounds, and the other will be strictly smaller than their upper bounds.让我们将 i 和 j 的最终 state 称为 k 和 l,其中 k 或 l 将等于它们各自的上限,而另一个将严格小于它们的上限。 This means this while loop has run k + l times这意味着这个 while 循环已经运行了 k + l 次

After the loop, you are performing another two mutually exclusive while loops, each of which starts at k/l stopped at until it reaches it's upper bound.在循环之后,您将执行另外两个互斥的 while 循环,每个循环都从 k/l 开始,停止直到它达到它的上限。 That means the two loops will run (arr.length - k) or (arr2.length - l) times.这意味着两个循环将运行 (arr.length - k) 或 (arr2.length - l) 次。

So the final results you have run is first while loop: (k + l) + second sets of loops:因此,您运行的最终结果是第一个 while 循环:(k + l) + 第二组循环:

   if k != arr.length (implies l == arr2.length): (arr.length - k) 
   if l != arr2.length (implies k == arr.length): (arr2.length - l)

so total is所以总数是

  if k != arr.length: (k + l) + (arr.length - k) = l + arr.length = arr2.length + arr.length
  if l != arr2.length: (k + l) + (arr2.length - l) = k + arr2.length = arr.length + arr2.length

both results in a total execution count of arr.length + arr2.length, hence the complexity is O(m + n)两者都导致 arr.length + arr2.length 的总执行计数,因此复杂度为 O(m + n)

well since it's only 2 arrays and the algorithm is straight forward, and you are iterating on both arr1 and arr2 on all indexes, the worst case is indeed O(n + m) where n=arr1.length and m=arr2.length.好吧,因为它只有 2 个 arrays 并且算法很简单,并且您正在对所有索引上的 arr1 和 arr2 进行迭代,最坏的情况确实是 O(n + m),其中 n=arr1.length 和 m=arr2.length。 but notice that this is not only the worst case but also the exact time needed meaning its also Θ(m+n)但请注意,这不仅是最坏的情况,而且是所需的确切时间,这也意味着它也是 Θ(m+n)

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

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