HeapSort(int data[ ], int n) {register int i; /* create a heap */ for (i = n/2 - 1; i >= 0; --i) /* LI: The nodes i+1,...,n/2-1 satisfy the heap property. */ move_down (data,i,n-1); /* move the root item to the end of the array data and restore the heap property to the tree data[0],...,data[i-2] by selecting the largest item */ for (i = n-1; i > 1; --i) { /* LI: The largest n-i-1 elements are in their proper places. data[0],...,data[i] is a heap containing the other elements. */ swap (data, data+i); move_down(data,0,i-1); } if (data[0] > data[1]) swap (data, data+1); }