Merge k sorted arrays | Set 1 | Java program to merge k sorted arrays of size n each . ; 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 . ; traverse the matrix ; sort the array ; A utility function to print array elements ; Driver program to test above functions
import java . io . * ; import java . util . * ; class GFG { public static void mergeKArrays ( int [ ] [ ] arr , int a , int [ ] output ) { int c = 0 ; for ( int i = 0 ; i < a ; i ++ ) { for ( int j = 0 ; j < 4 ; j ++ ) output [ c ++ ] = arr [ i ] [ j ] ; } Arrays . sort ( output ) ; } public static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 2 , 6 , 12 , 34 } , { 1 , 9 , 20 , 1000 } , { 23 , 34 , 90 , 2000 } } ; int k = 4 ; int n = 3 ; int [ ] output = new int [ n * k ] ; mergeKArrays ( arr , n , output ) ; System . out . println ( " Merged ▁ array ▁ is ▁ " ) ; printArray ( output , n * k ) ; } }