Hello,
I have to make a program using heapsort, and am having trouble getting it. I think the problem is with either fixup() or buildheap(). I am having trouble understanding the syntax for both of those methods, the teacher gave us the code for them, but I am having trouble getting it. Thanks!

Here is what I have so far:

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

class HeapSort
{
      private:
              int list[10];
      protected:
               void fixup(int, int, int);
               void buildheap();
      public:
             void heapsort();
             void store(int, int);
             void printHeap();
             
};

void
HeapSort::fixup(int value, int start, int last)
{
     int look;
     bool done;
     done = false;
     look = 2*start+1;
     
     while(look <= last && !done)
     {
          if(look < last)
          {
              if(list[look] < list[look+1])
                   look = look+1;
          }
          if(value > list[look])
              done = true;
          else
          {
              list[start] = list[look];
              start = look;
              look = 2*start+1;
          }
     }
}

void
HeapSort::buildheap()
{
     int count;
     int item;
     
     for(count = (10/2)-1; count >= 0; count--)
     {
          item = list[count];
          fixup(item, count, 9);          
     }             
}

void
HeapSort::heapsort()
{
     int count;
     int item;
     
     buildheap();
     for(count = 9; count >= 1; count--)
     {
          //cout << list[count] << endl;
          //cout <<"count " <<  count << endl;
          item = list[count];
          list[count] = list[0];
          fixup(item, 0, count-1);
          //cout << list[count] << endl;
     }
}

void
HeapSort::store(int number, int place)
{
    list[place] = number;      
}

void
HeapSort::printHeap()
{
     for(int i = 0; i < 10; i++)
         cout << list[i] << endl;                     
}
int
main()
{
     HeapSort heap1;
     HeapSort heap2;

     int x = 0;
     int numberInput;
     
     cout << "Enter ten numbers: " << endl;
     
     while(x < 10)
     {
          cin >> numberInput;
          //heap1.heapsort();
          //cout << x;
          heap1.store(numberInput, x);  
          x++;
     }
     
     x = 0;
     
     /*while(x < 10)
     {
         cin >> numberInput;
         heap2.store(numberInput, x);
         x++;
     }
     */
     heap1.heapsort();
     heap1.printHeap();
     //heap2.printHeap();
    // heap2.heapsort();
     //heap2.printHeap();
     
     system("pause");      
}

I will check that out, thanks!

I'm still having trouble with it. When I run my code, it outputs all the last number that I entered.

I have difficulty in creating a "stack" with the matter as follows:
1. 5 items in an item in 5 different boxes in the container A, how to move five boxes in the container B by using the "PUSH" and "POP"?
please help me in solving this problem in c + +!
thanks

Be a part of the DaniWeb community

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