sort.c
// max heap, number at i must be larger than its children
max heap, number at i must be larger than its children
max heap number at i must be larger than its children
void heapify(int array[], int n, int i) {
	int maxIndex = i;
	int leftChildIndex = 2 * i + 1;
	int rightChildIndex = leftChildIndex + 1;
	if (leftChildIndex < n && array[maxIndex] < array[leftChildIndex]) {
		maxIndex = leftChildIndex;
	}
	if (rightChildIndex < n && array[maxIndex] < array[rightChildIndex]) {
		maxIndex = rightChildIndex;
	}
	if (maxIndex != i) {
		swap(array + maxIndex, array + i);
		heapify(array, n, maxIndex);
	}
}
void heapify ( int array [ ] , int n , int i ) { int maxIndex = i ; int leftChildIndex = 2 * i + 1 ; int rightChildIndex = leftChildIndex + 1 ; if ( leftChildIndex < n && array [ maxIndex ] < array [ leftChildIndex ] ) { maxIndex = leftChildIndex ; } if ( rightChildIndex < n && array [ maxIndex ] < array [ rightChildIndex ] ) { maxIndex = rightChildIndex ; } if ( maxIndex != i ) { swap ( array + maxIndex , array + i ) ; heapify ( array , n , maxIndex ) ; } }
max heap, number at i must be larger than its children
max heap number at i must be larger than its children
// check if left child is larger // check if right child is larger // if number at i is not max, swap with max and heapify number at max