heap sort

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

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

heap sort

 
0
  #1
Feb 25th, 2009
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:
  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. look = 2*start+1;
  28.  
  29. while(look <= last && !done)
  30. {
  31. if(look < last)
  32. {
  33. if(list[look] < list[look+1])
  34. look = look+1;
  35. }
  36. if(value > list[look])
  37. done = true;
  38. else
  39. {
  40. list[start] = list[look];
  41. start = look;
  42. look = 2*start+1;
  43. }
  44. }
  45. }
  46.  
  47. void
  48. HeapSort::buildheap()
  49. {
  50. int count;
  51. int item;
  52.  
  53. for(count = (10/2)-1; count >= 0; count--)
  54. {
  55. item = list[count];
  56. fixup(item, count, 9);
  57. }
  58. }
  59.  
  60. void
  61. HeapSort::heapsort()
  62. {
  63. int count;
  64. int item;
  65.  
  66. buildheap();
  67. for(count = 9; count >= 1; count--)
  68. {
  69. //cout << list[count] << endl;
  70. //cout <<"count " << count << endl;
  71. item = list[count];
  72. list[count] = list[0];
  73. fixup(item, 0, count-1);
  74. //cout << list[count] << endl;
  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  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: heap sort

 
0
  #2
Feb 25th, 2009
Look at heapsort algorithm and implementation tutorial:
http://eternallyconfuzzled.com/tuts/...ting.aspx#heap
May be it helps...
Reply With Quote Quick reply to this message  
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

Re: heap sort

 
0
  #3
Feb 25th, 2009
I will check that out, thanks!
Reply With Quote Quick reply to this message  
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

Re: heap sort

 
0
  #4
Feb 25th, 2009
I'm still having trouble with it. When I run my code, it outputs all the last number that I entered.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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