Numbers from 1 to n are added to min-heap in a certain order. For each number find out how many times it changed its position in the min-heap.
Clarification: for addition use method Insert(), add nodes in the same order as they are in the input.
Input: In the first line is the number n. In the second line, divided by spaces, are n numbers from 1 to n.
Output: n numbers divided by spaces: i-th number indicates the number of position changes of the number i in the constructed min-heap.
i.e. 5 4 3 2 1
answer 2 3 3 2 2
public class MinHeap {
private int[] heap;
private int size;
private int maxsize;
public MinHeap(int maxsize) {
this.maxsize = maxsize;
this.size = 0;
heap = new int[this.maxsize + 1];
heap[0] = Integer.MIN_VALUE;
}
private void swap(int fpos, int spos) {
int tmp;
tmp = heap[fpos];
heap[fpos] = heap[spos];
heap[spos] = tmp;
}
private void minHeapify(int pos) {
if (2 * pos == size) {
if (heap[pos] > heap[2 * pos]) {
swap(pos, 2 * pos);
minHeapify(2 * pos);
}
return;
}
if (2 * pos <= size) {
if (heap[pos] > heap[2 * pos] || heap[pos] > heap[2 * pos + 1]) {
if (heap[2 * pos] < heap[2 * pos + 1]) {
swap(pos, 2 * pos);
minHeapify(2 * pos);
}
else {
swap(pos, 2 * pos + 1);
minHeapify(2 * pos + 1);
}
}
}
}
public void insert(int element) {
heap[++size] = element;
int current = size;
while (heap[current] < heap[current / 2]) {
swap(current, current / 2);
current = current / 2;
}
}
public void minHeap() {
for (int pos = (size / 2); pos >= 1; pos--) {
minHeapify(pos);
}
}
public int extractMin() {
if (size == 0) {
throw new NoSuchElementException("Heap is empty");
}
int popped = heap[1];
heap[1] = heap[size--];
minHeapify(1);
return popped;
}
}
I don't understand how to count