-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.js
executable file
·37 lines (31 loc) · 898 Bytes
/
matrix.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"use strict";
var Heap=require("./data_structures/heap").Heap;
var matrix = [
[20, 40, 80],
[5, 60, 90],
[45, 50, 55]
];
printSorted(matrix);
function printSorted(matrix){
var currentIndexes = new Array(matrix.length);
var N=matrix[0].length;
for(var i=0;i<currentIndexes.length;i++){
currentIndexes[i]=0;
}
var heap = new Heap(function(a,b){return a.key - b.key;});
for(var j=0;j<matrix.length;j++){
heap.add({key:matrix[j][currentIndexes[j]],row:j});
}
var printed = 0;
var retVal="";
while(printed < matrix.length*N){
heap.printArr();
var popped = heap.pop();
retVal+=popped.key+",";
printed++;
if(currentIndexes[popped.row]<(N-1)){
heap.add({key:matrix[popped.row][++(currentIndexes[popped.row])],row:popped.row});
}
}
console.log("retVal="+retVal);
}