943,920 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1016
  • C++ RSS
Feb 25th, 2009
0

Problem with heapsort

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. class HeapSort
  8. {
  9. private:
  10. int list[10];
  11. protected:
  12. void fixup(int, int, int);
  13. void buildheap();
  14. public:
  15. void heapsort();
  16. void store(int, int);
  17. void printHeap();
  18.  
  19. };
  20.  
  21. void
  22. HeapSort::fixup(int value, int start, int last)
  23. {
  24. int look;
  25. bool done;
  26. done = false;
  27. int temp;
  28. look = 2*start+1;
  29.  
  30. while(look <= last && !done)
  31. {
  32. if(look < last)
  33. {
  34. if(list[look] < list[look+1])
  35. look = look+1;
  36. }
  37. if(value > list[look])
  38. done = true;
  39. else
  40. {
  41. temp = list[start];
  42. list[start] = list[look];
  43. list[look] = temp;
  44. start = look;
  45. look = 2*start+1;
  46. }
  47. }
  48. }
  49.  
  50. void
  51. HeapSort::buildheap()
  52. {
  53. int count;
  54. int item;
  55.  
  56. for(count = 9/2; count >= 0; count--)
  57. {
  58. item = list[count];
  59. fixup(item, count, 9);
  60. }
  61. }
  62.  
  63. void
  64. HeapSort::heapsort()
  65. {
  66. int count;
  67. int item;
  68.  
  69. buildheap();
  70. for(count = 9; count >= 1; count--)
  71. {
  72. item = list[count];
  73. list[count] = list[0];
  74. fixup(item, 0, count-1);
  75. }
  76. }
  77.  
  78. void
  79. HeapSort::store(int number, int place)
  80. {
  81. list[place] = number;
  82. }
  83.  
  84. void
  85. HeapSort::printHeap()
  86. {
  87. for(int i = 0; i < 10; i++)
  88. cout << list[i] << endl;
  89. }
  90. int
  91. main()
  92. {
  93. HeapSort heap1;
  94. HeapSort heap2;
  95.  
  96. int x = 0;
  97. int numberInput;
  98.  
  99. cout << "Enter ten numbers: " << endl;
  100.  
  101. while(x < 10)
  102. {
  103. cin >> numberInput;
  104. //heap1.heapsort();
  105. //cout << x;
  106. heap1.store(numberInput, x);
  107. x++;
  108. }
  109.  
  110. x = 0;
  111.  
  112. /*while(x < 10)
  113.   {
  114.   cin >> numberInput;
  115.   heap2.store(numberInput, x);
  116.   x++;
  117.   }
  118.   */
  119. heap1.heapsort();
  120. heap1.printHeap();
  121. //heap2.printHeap();
  122. // heap2.heapsort();
  123. //heap2.printHeap();
  124.  
  125. system("pause");
  126. }
Similar Threads
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: max int value?
Next Thread in C++ Forum Timeline: Counting lines in a text file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC