/*reheapUp: This function is called by the heap sort to sort the array starting from the last element up. With an array implementation, this works from right to left. It continues using recursion. Input: randomArray(array of ints),newIndex(int),compares(int*),moves(int*) Return: Nothing*/ /*5*/void reheapUp(int randomArray[],int newIndex,int*compares,int*moves) { int parent; if(newIndex != 0){ parent = (newIndex-1)/2; if(*compares+=1,randomArray[newIndex] <= randomArray[parent]){ //Determines ascending or descending exchange(randomArray,newIndex,parent); *moves+=3; reheapUp(randomArray,parent,compares,moves); }//end if }//end if }//end reheapUp
void Heap::reheapUp(int root, int bottom) { int parent; if(bottom > root) { parent = (bottom - 1)/2; if(elements[parent] > elements[bottom]) { swap(parent, bottom); reheapUp(root, parent); } } }
/*sortHeap: This function will sort the array passed in with a heap sort. It will also count the number of compares and moves while the heapsort is running. Input: randomArray(array of ints),last(int),compares(int*),moves(int*) Return: Nothing*/ /*1*/void sortHeap(int randomArray[],int last,int*compares,int*moves) { int sorted; int walker = 1; while(walker<=last){ reheapUp(randomArray,walker,compares,moves); walker = walker+1; }//end while sorted = last; while(sorted > 0){ exchange(randomArray,0,sorted); *moves+=3; sorted = sorted-1; reheapDown(randomArray,0,sorted,compares,moves); }//end while }//end sortHeap
/*1*/void sortHeap(int randomArray[],int last,int*compares,int*moves) //heap is not sorting the first couple largest numbers { int sorted; int walker = 1; *compares = 0; *moves = 0; while(walker<=last){ reheapUp(randomArray,walker,compares,moves); walker = walker+1; } sorted = last; //sets sorted to last while(sorted > 0){ exchange(randomArray,0,sorted); *moves+=1; sorted = sorted-1; reheapDown(randomArray,0,sorted,compares,moves); } }