When dealing with multiple sorted arrays, a common challenge arises: how can we efficiently merge these arrays into a single sorted list? This problem is particularly relevant for situations where you continuously receive input in sorted order. In this post, we'll delve into a structured approach using a priority queue (also known as a min-heap) to achieve this efficiently.
Assume you have k sorted arrays, and you want to merge these into a single sorted array. For instance, consider the following arrays:
Your goal is to merge these arrays into one sorted array:
Output: [0, 1, 2, 4, 5, 6, 7, 8, 10]
Using a priority queue allows us to efficiently keep track of the smallest elements across the k arrays at any point in time. Here's a step-by-step breakdown of the approach:
Let’s implement this concept using Java:
import java.util.PriorityQueue; public class MergeKSortedArrays { static class ArrayEntry { int value; // The value of the element int arrayIndex; // The index of the array from which the value is taken int elementIndex; // The index of the element in its array public ArrayEntry(int value, int arrayIndex, int elementIndex) { this.value = value; this.arrayIndex = arrayIndex; this.elementIndex = elementIndex; } } public static int[] mergeKSortedArrays(int[][] arrays) { // Create a priority queue (min-heap) PriorityQueue<ArrayEntry> minHeap = new PriorityQueue<>((a, b) -> a.value - b.value); // Initialize the min-heap with the first element of each array for (int i = 0; i < arrays.length; i++) { if (arrays[i].length > 0) { minHeap.offer(new ArrayEntry(arrays[i][0], i, 0)); } } int totalElements = 0; for (int[] array : arrays) { totalElements += array.length; } int[] mergedArray = new int[totalElements]; int currentIndex = 0; while (!minHeap.isEmpty()) { // Get the smallest entry ArrayEntry smallest = minHeap.poll(); mergedArray[currentIndex++] = smallest.value; // If there are more elements in the same array, add the next element to the heap if (smallest.elementIndex + 1 < arrays[smallest.arrayIndex].length) { minHeap.offer(new ArrayEntry(arrays[smallest.arrayIndex][smallest.elementIndex + 1], smallest.arrayIndex, smallest.elementIndex + 1)); } } return mergedArray; } public static void main(String[] args) { int[][] arrays = { {1, 4, 7}, {2, 5, 8}, {0, 6, 10} }; int[] mergedResult = mergeKSortedArrays(arrays); for (int num : mergedResult) { System.out.print(num + " "); } } }
The efficiency of this algorithm becomes particularly beneficial when k (number of arrays) is small relative to N (number of total elements).
16/11/2024 | DSA
23/09/2024 | DSA
13/10/2024 | DSA
08/12/2024 | DSA
15/11/2024 | DSA
06/12/2024 | DSA
16/11/2024 | DSA
16/11/2024 | DSA
16/11/2024 | DSA
06/12/2024 | DSA
04/08/2024 | DSA