I just read the introduction to algorithms. I wrote code and the test was wrong... in the morning, I don't have time. I'll go to work right away. I'll post it here. I'll take a look at it later:
int GetParentIndex(int i){return (i+1)/2-1;}int GetLeftChild(int i){return 2*i+1;}int GetRightChild(int i){return 2*(i+1);}void MaxHeapfy(int* aArr,int aIndex,int aHeapSize){int lMaxIndex = aIndex;if (GetLeftChild(aIndex) < aHeapSize && aArr[GetLeftChild(aIndex)] > aArr[aIndex]){lMaxIndex = GetLeftChild(aIndex);}if (GetRightChild(aIndex) < aHeapSize && aArr[GetRightChild(aIndex)] > aArr[lMaxIndex]){lMaxIndex = GetRightChild(aIndex);}if (lMaxIndex != aIndex){int lTemp = aArr[lMaxIndex];aArr[lMaxIndex] = aArr[aIndex];aArr[aIndex] = lTemp;MaxHeapfy(aArr,lMaxIndex,aHeapSize);}}void BuildMaxHeap(int* aArr, int aSize){for (int i = (aSize)/2-1; i >= 0; --i){MaxHeapfy(aArr,i,aSize);}}void HeapSort(int* aArr, int aSize){BuildMaxHeap(aArr,aSize);int lHeapSize = aSize;for (int i = (aSize)/2-1; i >= 0; --i){int lTemp = aArr[i];aArr[i] = aArr[0];aArr[0] = lTemp;--lHeapSize;MaxHeapfy(aArr,i,lHeapSize);}}int main(){int lArr[] = {1,-10,26,8,-90};int lSize = sizeof(lArr)/sizeof(int);HeapSort(lArr,lSize);for (int i = 0; i < lSize; ++i){cout << lArr[i] << endl;}