using
System;
using
System.Collections.Generic;
class
MinHeapNode {
public
int
element;
public
int
i;
public
int
j;
public
MinHeapNode(
int
element,
int
i,
int
j) {
this
.element = element;
this
.i = i;
this
.j = j;
}
}
class
MinHeap {
public
int
heapSize;
public
MinHeapNode[] harr;
public
MinHeap(MinHeapNode[] a,
int
size) {
harr = a;
heapSize = size;
int
i = (heapSize - 1) / 2;
while
(i >= 0) {
minHeapify(i);
i--;
}
}
public
void
minHeapify(
int
i) {
int
l = left(i);
int
r = right(i);
int
smallest = i;
if
(l < heapSize && harr[l].element < harr[i].element) {
smallest = l;
}
if
(r < heapSize && harr[r].element < harr[smallest].element) {
smallest = r;
}
if
(smallest != i) {
MinHeapNode temp = harr[i];
harr[i] = harr[smallest];
harr[smallest] = temp;
minHeapify(smallest);
}
}
public
int
left(
int
i) {
return
2 * i + 1; }
public
int
right(
int
i) {
return
2 * i + 2; }
public
MinHeapNode getMin() {
return
harr[0]; }
public
void
replaceMin(MinHeapNode x) {
harr[0] = x;
minHeapify(0);
}
}
class
Program {
static
int
N = 4;
static
void
printSorted(
int
[][] mat) {
MinHeapNode[] harr =
new
MinHeapNode[N];
for
(
int
i = 0; i < N; i++) {
harr[i] =
new
MinHeapNode(mat[i][0], i, 1);
}
MinHeap heap =
new
MinHeap(harr, N);
for
(
int
count = 0; count < N * N; count++) {
MinHeapNode root = heap.getMin();
Console.Write(root.element +
" "
);
if
(root.j < N) {
root.element = mat[root.i][root.j];
root.j += 1;
}
else
{
root.element =
int
.MaxValue;
}
heap.replaceMin(root);
}
}
static
void
Main(
string
[] args) {
int
[][] mat = {
new
int
[]{10, 20, 30, 40},
new
int
[]{15, 25, 35, 45},
new
int
[]{27, 29, 37, 48},
new
int
[]{32, 33, 39, 50}
};
printSorted(mat);
}
}