Read the following Heap Sort function and determine the correct answer for blank
ID: 3900412 • Letter: R
Question
Read the following Heap Sort function and determine the correct answer for blank #3.
template<class ItemType>
void HeapSort(ItemType values[], int numValues)
// Pre: Struct HeapType is available.
// Post: The elements in the array values are sorted by key.
{
int index;
// Convert the array of values into a heap.
for (index = ___________; index >= 0; index--) // 1
_________(values, index, numValues-1); // 2
// Sort the array.
for (index = numValues-1; index >=1; index--)
{
Swap(___________, values[index]); // 3
__________(values, 0, index-1); // 4
}
}
A) values[numValues-1]
B) values[0]
C) values[1]
Explanation / Answer
option B Values[0].
After every heapify that is after creating a heap we aill extract element one by one and we will move the element at the index to the root, since root is at index 0 we are swapping the element at inex i with index at element 0 that is root.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.