| | |
heap sort
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 108
Reputation:
Solved Threads: 1
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:
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:
C++ Syntax (Toggle Plain Text)
#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"); }
Look at heapsort algorithm and implementation tutorial:
http://eternallyconfuzzled.com/tuts/...ting.aspx#heap
May be it helps...
http://eternallyconfuzzled.com/tuts/...ting.aspx#heap
May be it helps...
![]() |
Similar Threads
- heap sort (C)
- Trouble with heap sort (Computer Science)
- Narue's Heap sort a further explanation? (C)
- Heap sort, a little help (C)
- erge and Heap...which is really faster (C++)
Other Threads in the C++ Forum
- Previous Thread: max int value?
- Next Thread: Problem with heapsort
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






