Hello,
I am getting 8 8 8 7 7 8 6 7 8 9
as output when I enter 1 2 3 4 5 6 7 8 9 0 into this program. It is supposed to sort the numbers one through 10. I debugged it, and believe the problem is called in heapsort() when it calls fixup(). Before then the program seems to work fine.
I am not sure how to fix this.
here is my code:
#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;
int temp;
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
{
temp = list[start];
list[start] = list[look];
list[look] = temp;
start = look;
look = 2*start+1;
}
}
}
void
HeapSort::buildheap()
{
int count;
int item;
for(count = 9/2; count >= 0; count--)
{
item = list[count];
fixup(item, count, 9);
}
}
void
HeapSort::heapsort()
{
int count;
int item;
buildheap();
for(count = 9; count >= 1; count--)
{
item = list[count];
list[count] = list[0];
fixup(item, 0, count-1);
}
}
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");
}