-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEfficientArrayMergeUsingAHeap.java
167 lines (136 loc) · 3.79 KB
/
EfficientArrayMergeUsingAHeap.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//Pawel Adamczuk - 1
//Merge 'n' sorted Integer arrays not longer than 'm' in O(m*n*log(n)) time, using O(n) memory
import java.util.Scanner;
public class Source {
private static class MinHeap {
private primitiveStack[] mainArray;
private int currentRange;
public MinHeap(int size) {
currentRange = -1;
mainArray = new primitiveStack[size];
}
public int pop() {
int temp = mainArray[0].returnTop();
if ( mainArray[0].decrease() )
{//exists
heapify(0);
}
else
{//ceases to exist
swap(0, currentRange);
--currentRange;
heapify(0);
}
return temp;
}
public void heapify (int i)
{
int l = leftChild(i);
int r = rightChild(i);
int smallest;
if (l <= currentRange && mainArray[l].returnTop() < mainArray[i].returnTop())
smallest = l;
else
smallest = i;
if (r <= currentRange && mainArray[r].returnTop() < mainArray[smallest].returnTop())
smallest = r;
if (smallest != i)
{
swap(i, smallest);
heapify(smallest);
}
}
public void addElement(primitiveStack elem) {
++currentRange;
mainArray[currentRange] = elem;
}
public void buildHeap() {
for (int i = parent(currentRange); i >= 0; --i)
{
heapify(i);
}
}
private int parent(int i)
{
return (i - 1) / 2;
}
private int leftChild(int i)
{
return 2 * i + 1;
}
private int rightChild(int i)
{
return 2 * i + 2;
}
private void swap(int a, int b) {
primitiveStack temp = mainArray[a];
mainArray[a] = mainArray[b];
mainArray[b] = temp;
}
}
private class EmptyStackException extends Exception {
}
private static class primitiveStack {
private int top;
public int[] arr;
public primitiveStack (int[] arr) {
this.arr = arr;
this.top = 0;
}
public boolean decrease() { // true - stack exists, false - stack empty
++top;
if (top < arr.length)
return true;
else
return false;
}
public int returnTop() /*throws EmptyStackException*/ {
// if (top > -1)
// {
return this.arr[top];
// }
// else
// throw new EmptyStackException();
}
}
public Source() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner mainScanner = new Scanner(System.in);
StringBuilder tempStr = new StringBuilder("");
int setCount = mainScanner.nextInt();
int length;
for (int i = 0; i < setCount; ++i)
{
int sequenceCount = mainScanner.nextInt();
int elemCount = 0;
int[] lengths = new int[sequenceCount];
for (int j = 0; j < sequenceCount; ++j)
{
lengths[j] = mainScanner.nextInt();
elemCount += lengths[j];
}
MinHeap mainHeap = new MinHeap(sequenceCount);
for (int j = 0; j < sequenceCount; ++j)
{
int[] tempArr = new int[lengths[j]];
for (int k = 0; k < lengths[j]; ++k)
{
tempArr[k] = mainScanner.nextInt();
}
primitiveStack tempStack = new primitiveStack(tempArr);
mainHeap.addElement(tempStack);
}
mainHeap.buildHeap();
for (int l = 0; l < elemCount; ++l)
{
tempStr.append(mainHeap.pop());
tempStr.append(" ");
}
System.out.println(tempStr);
tempStr.setLength(0);
}
}
}