public int DelMin() { var item = pq[1]; SortUtil.Exchange(pq, 1, N); qp[pq[1]] = 1; qp[pq[N]] = N; N--; Sink(1); return(item); }
private void Swim(int k) { while (k > 1) { var parent = k / 2; if (SortUtil.IsLessThan(keys[pq[k]], keys[pq[parent]])) { SortUtil.Exchange(pq, k, parent); qp[pq[k]] = k; qp[pq[parent]] = parent; k = parent; } else { break; } } }
private void Sink(int k) { while (k * 2 <= N) { int child = 2 * k; if (child < N && SortUtil.IsLessThan(keys[pq[child + 1]], keys[pq[child]])) { child++; } if (SortUtil.IsLessThan(keys[pq[child]], keys[pq[k]])) { SortUtil.Exchange(pq, child, k); qp[pq[child]] = child; qp[pq[k]] = k; k = child; } else { break; } } }