Hint: Heapsort

Question: Heapify the following using the algorithm we outlined in class. The heap should have the maximum element at the top of the heap.

  35 57 53 26 50 15 22 21 25 14 11  2

Hint: Here is the code given in class for heapifying.

void heapify(int* A, int len) {
	for(int i = len / 2 - 1; i >= 0; i--) siftUp(A, len, i);
}

void siftUp(int* A, int len, int i) {
	while(1) {
		int child = 2 * i + 1;
		if(child >= len) return; // at bottom of heap
		if(child + 1 < len && A[child] < A[child + 1]) child++;
		if(A[i] >= A[child]) return; // done
		int temp = A[i]; // swap
		A[i] = A[child];
		A[child] = temp;
		i = child;
	}
}

Answer.


Hint: Heapsort / Arrays algorithms / Review questions / 15-211 A, B