| | |
Problem with heapsort
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 108
Reputation:
Solved Threads: 1
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:
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:
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; 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"); }
![]() |
Similar Threads
- Why am I getting a StackOverflowError? (Java)
- Problem with memory allocation (C++)
- Insertion Sort Problem (C++)
- Heap sort, a little help (C)
- Quick, Insertion, and Partition (C++)
Other Threads in the C++ Forum
- Previous Thread: heap sort
- Next Thread: Counting lines in a text file
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux 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 return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





