Posted By

akljaic on 01/19/15


Tagged

tree binary


Versions (?)

hrpa.h


 / Published in: C++
 

URL: asdf

asdf

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void max_heapify(int *a, int i, int n){
  5. int j,temp;
  6. temp = a[i];
  7. j = 2*i;
  8. while(j <= n){
  9. if(j < n && a[j+1] > a[j])
  10. j = j+1;
  11. if(temp > a[j])
  12. break;
  13. else if(temp <= a[j]){
  14. a[j/2] = a[j];
  15. j = 2*j;
  16. }
  17. }
  18. a[j/2] = temp;
  19. return;
  20. }
  21.  
  22. void heapsort(int *a, int n){
  23. int temp;
  24. for(int i = n; i>=2; i--){
  25. temp = a[i];
  26. a[i] = a[1];
  27. a[1] = temp;
  28. max_heapify(a, 1, i-1);
  29. }
  30. }
  31.  
  32. void build_maxheap(int *a, int n){
  33. for(int i = n/2; i>=1; i--){
  34. max_heapify(a,i,n);
  35. }
  36. }

Report this snippet  

You need to login to post a comment.