Priority Queue (sink operations)

450 Views Asked by At

I am practising on priority queue topic from Algorithms book by robert sedgwick. In a question we have to implement sink() method with out j < N condition.

private void sink(int k){
    while (2*k <= N){
         int j = 2*k;
         if (j < N && less(j, j+1)) j++;
         if (!less(k, j)) break;
         exch(k, j);
         k = j;
    }
}

private boolean less(int i, int j){ 
    return pq[i].compareTo(pq[j]) < 0;
}

private void exch(int i, int j){ 
    Key t = pq[i]; 
    pq[i] = pq[j]; 
    pq[j] = t;
}

Above is code from book by Robert sedgwick check page no. 329 and question no 2.4.13

0

There are 0 best solutions below