📜  合并 k 个排序数组 |设置 2(不同大小的阵列)

📅  最后修改于: 2021-10-28 02:06:52             🧑  作者: Mango

给定 k 个可能不同大小的排序数组,合并它们并打印排序后的输出。
例子:

Input: k = 3 
      arr[][] = { {1, 3},
                  {2, 4, 6},
                  {0, 9, 10, 11}} ;
Output: 0 1 2 3 4 6 9 10 11 

Input: k = 2
      arr[][] = { {1, 3, 20},
                  {2, 4, 6}} ;
Output: 1 2 3 4 6 20 

我们在 Merge k sorted arrays | 中讨论了一个适用于所有相同大小数组的解决方案。设置 1。
一个简单的解决方案是创建一个输出数组,然后将所有数组一一复制到其中。最后,使用排序输出数组。这种方法需要 O(N Logn N) 时间,其中 N 是所有元素的计数。
一个有效的解决方案是使用堆数据结构。基于堆的解决方案的时间复杂度是 O(N Log k)。
1. 创建一个输出数组。
2. 创建一个大小为 k 的最小堆并将所有数组中的第一个元素插入堆中
3. 优先级队列不为空时重复以下步骤。
…..a) 从堆中移除最小元素(最小值总是在根)并将其存储在输出数组中。
…..b) 从提取元素的数组中插入下一个元素。如果数组没有更多元素,则什么都不做。

C++
// C++ program to merge k sorted arrays
// of size n each.
#include 
using namespace std;
 
// A pair of pairs, first element is going to
// store value, second element index of array
// and third element index in the array.
typedef pair > ppi;
 
// This function takes an array of arrays as an
// argument and all arrays are assumed to be
// sorted. It merges them together and prints
// the final sorted output.
vector mergeKArrays(vector > arr)
{
    vector output;
 
    // Create a min heap with k heap nodes. Every
    // heap node has first element of an array
    priority_queue, greater > pq;
 
    for (int i = 0; i < arr.size(); i++)
        pq.push({ arr[i][0], { i, 0 } });
 
    // Now one by one get the minimum element
    // from min heap and replace it with next
    // element of its array
    while (pq.empty() == false) {
        ppi curr = pq.top();
        pq.pop();
 
        // i ==> Array Number
        // j ==> Index in the array number
        int i = curr.second.first;
        int j = curr.second.second;
 
        output.push_back(curr.first);
 
        // The next element belongs to same array as
        // current.
        if (j + 1 < arr[i].size())
            pq.push({ arr[i][j + 1], { i, j + 1 } });
    }
 
    return output;
}
 
// Driver program to test above functions
int main()
{
    // Change n at the top to change number
    // of elements in an array
    vector > arr{ { 2, 6, 12 },
                              { 1, 9 },
                              { 23, 34, 90, 2000 } };
 
    vector output = mergeKArrays(arr);
 
    cout << "Merged array is " << endl;
    for (auto x : output)
        cout << x << " ";
 
    return 0;
}


Python
# merge function merge two arrays
# of different or same length
# if n = max(n1, n2)
# time complexity of merge is (o(n log(n)))
 
from heapq import merge
 
# function for meging k arrays
def mergeK(arr, k):
     
    l = arr[0]
    for i in range(k-1):
         
        # when k = 0 it merge arr[1]
        # with arr[0] here in l arr[0]
        # is stored
        l = list(merge(l, arr[i + 1]))
    return l
 
# for printing array
def printArray(arr):
    print(*arr)
 
 
# driver code
arr =[[2, 6, 12 ],
    [ 1, 9 ],
    [23, 34, 90, 2000 ]]
k = 3
 
l = mergeK(arr, k)
 
printArray(l)


C#
// C# program to merge k sorted arrays
// of size n each. 
using System;
using System.Collections.Generic;
class GFG
{
  
    // This function takes an array of arrays as an
    // argument and all arrays are assumed to be
    // sorted. It merges them together and prints
    // the final sorted output.
    static List mergeKArrays(List> arr)
    {
        List output = new List();
       
        // Create a min heap with k heap nodes. Every
        // heap node has first element of an array
        List>> pq =
          new List>>();
       
        for (int i = 0; i < arr.Count; i++)
            pq.Add(new Tuple>(arr[i][0],
                                   new Tuple(i, 0)));
             
        pq.Sort();
       
        // Now one by one get the minimum element
        // from min heap and replace it with next
        // element of its array
        while (pq.Count > 0) {
            Tuple> curr = pq[0];
            pq.RemoveAt(0);
       
            // i ==> Array Number
            // j ==> Index in the array number
            int i = curr.Item2.Item1;
            int j = curr.Item2.Item2;
       
            output.Add(curr.Item1);
       
            // The next element belongs to same array as
            // current.
            if (j + 1 < arr[i].Count)
            {
                pq.Add(new Tuple>(arr[i][j + 1],
                                                     new Tuple(i, j + 1)));
                pq.Sort();
            }
        }
        return output;
    }   
     
  // Driver code
  static void Main()
  {
     
    // Change n at the top to change number
    // of elements in an array
    List> arr = new List>();
    arr.Add(new List(new int[]{2, 6, 12}));
    arr.Add(new List(new int[]{1, 9}));
    arr.Add(new List(new int[]{23, 34, 90, 2000}));
   
    List output = mergeKArrays(arr);
   
    Console.WriteLine("Merged array is ");
    foreach(int x in output)
        Console.Write(x + " ");
  }
}
 
// This code is contributed by divyeshrabadiya07.


输出:
Merged array is 
1 2 6 9 12 23 34 90 2000

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程