heapify method

void heapify(
  1. List<num> list,
  2. num length,
  3. num i
)

Implementation

void heapify(List<num> list, num length, num i) {
  var largest = i, left = i * 2 + 1, right = i * 2 + 2;

  // check if root is less than left child
  if (left < length && list[i as int] < list[left as int]) {
    largest = left;
  }

  // check if root is less than right child
  if (right < length && list[largest as int] < list[right as int]) {
    largest = right;
  }

  if (largest != i) {
    // ---- start ---- do the switching of the child with the root.
    var temp = list[i as int];
    list[i] = list[largest as int];
    list[largest] = temp;
    // ----- end -----
    heapify(list, length, largest);
  }
}