Advertisement
Guest User

Untitled

a guest
May 4th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. #include<iostream.h>
  2. #include<stdlib.h>
  3. /*This code is written by NF between any time of 120711 and 210711;modified at 231211*/
  4.  
  5. int main()
  6. {
  7. int length=0,min_heapified=0,start_length=0,index=0;
  8. cout<<"Enter Array Length:";
  9. cin>>length;
  10. int array[length];
  11. for(int i=0;i<length;i++)
  12. {
  13. array[i]=rand()%100;
  14. }
  15. cout<<"Array is filled now with random elements";
  16. cout<<"nSorted Array:"<<endl;
  17. do
  18. {
  19. do
  20. {
  21. min_heapified=0;
  22. for(int root=0;root<length/2;root++)/*As the leaf nodes have no child they should not be in a consideration while swapping so looping upto length/2 is enough*/
  23. {
  24.  
  25. int left=((2*root)+1);
  26. int right=((2*root)+2);
  27.  
  28. if(left<length)
  29. {
  30. if(array[left]<array[root])
  31. {
  32. int swap=array[left];
  33. array[left]=array[root];
  34. array[root]=swap;
  35. min_heapified=1;
  36. }
  37. }
  38. if(right<length)
  39. {
  40. if(array[right]<array[root])
  41. {
  42. int swap=array[right];
  43. array[right]=array[root];
  44. array[root]=swap;
  45. min_heapified=1;
  46. }
  47. }
  48.  
  49. }
  50.  
  51. }while(min_heapified>0);
  52.  
  53. int swap=array[0];
  54. array[0]=array[--length];/*modification done at this point to avoid keeping the sorted elements into another array;and swapping done between the 0th and last element*/
  55. array[length]=swap;
  56. cout<<array[length]<<" ";
  57.  
  58. }while(length>0);
  59.  
  60. return 0;
  61.  
  62. }
  63.  
  64. void heapsort(int *begin, int *end)
  65. {
  66. // assume end is one pass the last element. eg. arry + length;
  67. heapify(begin, end);
  68.  
  69. while(begin != --end)
  70. {
  71. swap(*begin, *end); // invariant: the next item to order will be at the top
  72. // after the swap, the next biggest/smallest item may not be
  73. // at the top. use push_down to fix this and preserve the heap property.
  74. push_down(begin, end);
  75. }
  76. }
  77.  
  78. void push_down(int *begin, int *end, size_t defender = 0)
  79. {
  80. // at the very bottom?
  81. if(defender >= std::distance(begin, end) / 2) return;
  82.  
  83. size_t challenger = defender * 2 + 1;
  84. // is there a right-child?
  85. // Note that in even# arrays, last parent doesn't have a right child
  86. if(begin + challenger + 1 != end)
  87. if(begin[challenger + 1] < begin[challenger])
  88. ++challenger;
  89.  
  90. // defended successfully?
  91. if( !(begin[challenger] < begin[defender]) ) return;
  92.  
  93. // challenger wins
  94. std::swap(begin[challenger], begin[defender]);
  95. push_down(begin, end, challenger);
  96. }
  97.  
  98. int length=0,min_heapified=0,start_length=0,index=0;
  99.  
  100. do {
  101. int min_heapified = 0;
  102. do {
  103. ...
  104.  
  105. if(x<=(length-1))
  106.  
  107. if (x < length)
  108.  
  109. int swap=array[left];
  110. array[left]=array[root];
  111. array[root]=swap;
  112.  
  113. int swap(array, index, rootIndex) {
  114. int swap = array[index];
  115. array[index] = array[rootIndex];
  116. array[root] = swap;
  117. }
  118.  
  119. if(array[left]<array[root])
  120. {
  121. int swap=array[left];
  122. array[left]=array[root];
  123. array[root]=swap;
  124. min_heapified=1;
  125. }
  126.  
  127. int swapIfLess(array, index, rootIndex) {
  128. if (array[index] < array[rootIndex]) {
  129. swap(array, index, rootIndex)
  130. return 1;
  131. }
  132. return 0;
  133. }
  134.  
  135. if (left < length) {
  136. if (swapIfLess(array, left, root)) {
  137. min_heapified=1;
  138. }
  139. }
  140. if (right < length) {
  141. if (swapIfLess(array, right, root)) {
  142. min_heapified=1;
  143. }
  144. }
  145.  
  146. int root=i;
  147.  
  148. for (int root = 0; root < length; root++)
  149.  
  150. int length=0,min_heapified=0,start_length=0,index=0;
  151.  
  152. int array[length],sorted_array[length];
  153.  
  154. if(root <= (length-1))
  155. // Prefer the following
  156. if(root < length)
  157.  
  158. if(root < length)
  159.  
  160. for(int root=0;root<length;root++)
  161. // prefer
  162. for(int root=0;root<length;++root)
  163.  
  164. min_heapified=0;
  165.  
  166. bool min_heapified = false;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement