public void MaxHeapify(Heap <int> A, int i) { int largest = i; int left = A.Left(i); if (left <= A.HeapSize && A[largest] < A[left]) { largest = left; } int right = A.Right(i); if (right <= A.HeapSize && A[largest] < A[right]) { largest = right; } if (largest != i) { int temp = A[i]; A[i] = A[largest]; A[largest] = temp; MaxHeapify(A, largest); } }
public void MaxHeapifyLoop(Heap <int> A, int i) { int largest = i; while (!A.IsLeaf(largest)) { int left = A.Left(i); if (left <= A.HeapSize && A[largest] < A[left]) { largest = left; } int right = A.Right(i); if (right <= A.HeapSize && A[largest] < A[right]) { largest = right; } if (largest != i) { int temp = A[i]; A[i] = A[largest]; A[largest] = temp; } else { break; } } }