Merge K sorted arrays | Set 3 ( Using Divide and Conquer Approach ) | Javascript program to merge K sorted arrays ; Merge and sort k arrays ; Put the elements in sorted array . ; Sort the output array ; Input 2D - array ; Number of arrays ; Output array ; Print merged array
let N = 4 ; function merge_and_sort ( output , arr , n , k ) { for ( let i = 0 ; i < k ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { output [ i * n + j ] = arr [ i ] [ j ] ; } } output . sort ( function ( a , b ) { return a - b } ) ; } let arr = [ [ 5 , 7 , 15 , 18 ] , [ 1 , 8 , 9 , 17 ] , [ 1 , 4 , 7 , 7 ] ] ; let n = N ; let k = 3 ; let output = new Array ( n * k ) ; merge_and_sort ( output , arr , n , k ) ; for ( let i = 0 ; i < n * k ; i ++ ) document . write ( output [ i ] + " " ) ;