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; } } }
public void Enqueue(int index, T item) { var p = qp[index]; if (p == -1) { Insert(index, item); } else { if (SortUtil.IsLessThan(keys[index], item)) { keys[index] = item; Sink(p); } else { keys[index] = item; Swim(p); } } }
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; } } }