/* library for output */ #include /* libraries for the random number generator */ #include #include using namespace std; // heapsort code works for all data types, // including user defined types, // provided operator '>' is overloaded /* function prototypes*/ template void percDown(T array [], int hole, int size) ; template void printArray(T array [], int length) { cout << "["; for (int i=0 ; i void swapReferences(T array [], int i, int j) { T tmp = array[i]; array[i] = array[j]; array[j] = tmp; } /** Heap sort method. Modifies array so that it is in sorted order */ template void heapSort(T array [], int length) { // Build Heap for (int i=length/2 ; i>=0 ; i-- ) { percDown(array, i, length); } //printArray(array, length); for (int i=length-1 ; i>0 ; i-- ) { swapReferences( array, 0, i ); // deleteMax percDown( array, 0, i); // Maintain heap ordering property } } /** Percolate down method. The object to be percolated is at * array[hole], and the current heap size (number of elements in * the heap) is given. */ template void percDown(T array [], int hole, int size) { // YOUR CODE HERE } /** Test driver method. */ int main(int argc, char * argv []) { //------------------------------------------------- cout << "\nTest 1: array of 39 random integers\n"; int arrSize = 39; int a[arrSize]; /* initialize random number generator */ /* change the seed to the random number generator, based on the time of day */ srand (time ((time_t *) 0) ); for (int i=0 ; i(a, arrSize); heapSort(a, arrSize); cout<< "After:\n"; printArray(a, arrSize); //------------------------------------------------- cout << "\n\nTest 2: array of 26 letters in descending order\n"; /* initialize character array */ char alpha[27] = "zyxwvutsrqponmlkjihgfedcba"; cout << "Before: \n"; printArray(alpha, 26); heapSort(alpha, 26); cout<< "After:\n"; printArray(alpha, 26); //------------------------------------------------- cout << "\n\nTest 3: array of 25 doubles in ascending order\n"; /* initialize double array */ double reals[25]; for (int i=0 ; i(reals, 26); heapSort(reals, 26); cout<< "After:\n"; printArray(reals, 26); }