Example #1
0
/*reheapDown: This function is called by heap sort to sort the
 array and is sort of like the opposite of reheapUp. It starts from
 the very top of the heap (left in array implementation) and then
 sorts towards the right, comparing parent with its largest child
 node(smallest in our case as we are doing descending order).
 Input: randomArray(array of ints),root(int),last(int),compares(int*),moves(int*)
 Return: Nothing*/
/*6*/void reheapDown(int randomArray[],int root,int last,int*compares,int*moves)
{
    int leftKey,rightKey,smallChildIndex;
    
    if((root*2+1) <= last){
        leftKey = randomArray[root*2+1];
        *moves+=1;
        if((root*2+2) <= last){
            rightKey = randomArray[root*2+2];
            *moves+=1;
        }//end if
        else{
            rightKey = 7501;
        }//end else
        if(*compares+=1,leftKey < rightKey)  //Determines ascending or descending
            smallChildIndex = root*2+1;
        else
            smallChildIndex = root*2+2;
        if(*compares+=1,randomArray[root] > randomArray[smallChildIndex]) //Determines ascending or descending
        {
            exchange(randomArray,root,smallChildIndex);
            *moves+=3;
            reheapDown(randomArray,smallChildIndex,last,compares,moves);
        }//end if
    }//end if
}//end reheapDown
Example #2
0
void Heap::reheapDown(int root, int bottom)
{
   int minChild, rightChild, leftChild;

   leftChild = 2*root+1;
   rightChild = 2*root+2;

   if(leftChild <= bottom)
   {  // left child is part of the heap
       if(leftChild == bottom) // only one child
            minChild = leftChild;
       else 
       { // two children
            if(elements[leftChild] >= elements[rightChild])
                 minChild = rightChild;
            else
                 minChild = leftChild;
       }
   if(elements[root] > elements[minChild]) 
   {
       swap(root, minChild);
       reheapDown(minChild, bottom);
   }
  }
} 
Example #3
0
/*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
Example #4
0
/*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);
	}
}
Example #5
0
/*6*/void reheapDown(int randomArray[],int root,int last,int*compares,int*moves)
{
    int leftKey,rightKey,largeChildIndex;
    
    if((root*2+1) <= last){
        leftKey = randomArray[root*2+1];
        if((root*2+2) <= last)
            rightKey = randomArray[root*2+2];
        else
            rightKey = 7500;
        if(leftKey < rightKey)  //Determines ascending or descending
            largeChildIndex = root*2+1;
        else
            largeChildIndex = root*2+2;
        if(*compares+=1,randomArray[root] > randomArray[largeChildIndex]) //Determines ascending or descending
        {
            exchange(randomArray,root,largeChildIndex);
            *moves+=1;
            reheapDown(randomArray,largeChildIndex,last,compares,moves);
        }
    }
    
    
}