bigmaq 0 Newbie Poster

So I am programming a heap sort and i am gettin pretty close to where i need to be, but I am having trouble in one of my functions when it heapifies my array. here is the code.

#include<iostream>
using namespace std;
    
class Heapsort
{
  private:
     int *array;
     int mysize;
  public:
     Heapsort(int);
     ~Heapsort();
     bool empty();
     int percolate_down(int);
     void heapify();
     void fill_array();
     void print();
};
    
Heapsort::Heapsort(int a)
{
   mysize = a;
   int *array = new int[mysize];
}
          
Heapsort::~Heapsort()
{
   delete [] array;
}
          
void Heapsort::fill_array()
{
    int i;
     
    for (i = 1; i < mysize; i++){
       cout << "\nEnter a number: ";
       cin >> array[i];
    }
 
    print();    //This is here to check for the error im getting.
}
 
int Heapsort::percolate_down(int r)
{  
    int t=0;
    int c = 2*r;
    
    while (r < mysize){
        if ((c < mysize) && (array[c] < array[c+1])){
          c++;
        }
        if (array[r] < array[c]){
          t = array[c];
          array[c] = array[r];
          array[r] = t;
          r=c;
          c=2*c;
          print();  //Again, checking the error, the print out shows that
        }else{    //the data somehow changes in the middle of loop
          break;
        }
    }  
    return (r);
}

void Heapsort::heapify()
{  
   int r = mysize/2;   
   
   for(r; r > 0; r--){
      percolate_down(r);
   }    
 
}        

void Heapsort::print()
{ 
  int j;
  cout << endl << endl;
  for (j=1; j < mysize; j++)
        cout << array[j] << "  ";
}
        
int main()
{
  Heapsort heap(7);
  
  cout << endl << endl;
  
  heap.fill_array();
 
  heap.heapify();

  heap.print();
   
  cout << endl << endl;
   
  return (0);
}

And this is the out put i am getting.

AFTER I INPUT THE INTEGERS INTO ARRAY:
1 2 3 4 5 6

DURING MY WHILE LOOP IN PERCOLATE_DOWN():
1 2 6 4 5 3

1 5 6 4 2 3

1 5 6 4 671410272 3

6 5 1 4 671410272 3

6 5 3 4 671410272 1

6 5 3 4 671410272 1


I have no idea why in the middle of the loop, the data point just changes.

p.s. I havnt gotten to the sorting part, this is just up to put the data into a heap.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.