Problem with heapsort

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Problem with heapsort

 
0
  #1
Feb 25th, 2009
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:
  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. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC