I am trying to make the heapSort function I wrote work with the heapRebuild I was given.
If someone could please tell me what exactly I am doing wrong or help point me in the right direction.
void Heap::heapRebuild(int root)
{
// if the root is not a leaf and the root's search key
// is less than the larger of the search keys in the
// root's children
int child = 2 * root + 1; // index of root's left
// child, if any
if ( child < size )
{ // root is not a leaf, so it has a left child at child
int rightChild = child + 1; // index of right child,
// if any
// if root has a right child, find larger child
if ( (rightChild < size) &&
(items[rightChild] > items[child]) )
child = rightChild; // index of larger child
// if the root's value is smaller than the
// value in the larger child, swap values
if (items[root] < items[child])
{ HeapItemType temp = items[root];
items[root] = items[child];
items[child] = temp;
// transform the new subtree into a heap
heapRebuild(child);
} // end if
} // end if
// if root is a leaf, do nothing
} // end heapRebuild
void Heap::heapSort()
{
int temp;
int last = size-1;
for(int i = 0; i < size; i++)
{
temp = items[0];
items[0] = items[last];
items[last] = temp;
last--;
heapRebuild(0);
}
}