public class PriorityQueue {
public static int[] arr = {3, 2, 4, 1, 6, 8, 0};
int[] a;
int N = 0;
public PriorityQueue(int maxN) {
a = new int[maxN + 1];
}
private void exch(int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
private void swim(int k) {
while(k > 1 && a[k] > a[k/2]) {
exch(k, k/2);
k = k/2;
}
}
public void insert(int i) {
a[++N] = i;
swim(N);
}
private void sink(int k) {
while(k*2 <= N) {
int j = 2 * k;
if(j < N && a[j] < a[j + 1]) j++;
if(a[k] > a[j]) break;
exch(k, j);
k = j;
}
}
public int detetMax() {
int r = a[1];
exch(1, N--);
sink(1);
return r;
}
public int size() {
return N;
}
public static void main(String[] args) {
PriorityQueue q = new PriorityQueue(arr.length);
for (int i: arr) {
q.insert(i);
}
while(q.size()>0) {
System.out.println(q.detetMax());
}
}
}