Posted By

cindri_marko on 01/19/15


Tagged

sort heap


Versions (?)

heap_sort.h


 / Published in: C++
 

heap_sort.h

  1. #include <iostream>
  2.  
  3. typedef int tip;
  4. void HeapSort(int a[], int n);
  5. void Podesi(int a[], int n, int i);
  6. void StvoriGomilu(int a[], int n);
  7.  
  8.  
  9.  
  10. void Podesi(int a[], int n, int i){
  11. int j,t;
  12.  
  13. j = 2 * i + 1;
  14. t = a[i];
  15.  
  16. while(j<n)
  17. {
  18. if(j < (n-1) && a[j] < a[j+1])j++;
  19. if(t >= a[j])break;
  20.  
  21. a[i] = a[j];
  22. i = j;
  23. j = 2*j+1;
  24. }
  25.  
  26. a[i] = t;
  27. }
  28.  
  29. void CreateHeap(int a[], int n){
  30. int i;
  31. for(i=(n-2)/2;i>=0;i--)Podesi(a,n,i);
  32. }
  33.  
  34. void HeapSort(int a[], int n){
  35. int i,t;
  36.  
  37. CreateHeap(a,n);
  38. for(i=n;i>=2;i--)
  39. {
  40. t = a[i-1];
  41. a[i-1] = a[0];
  42. a[0] = t;
  43.  
  44. Podesi(a,i-1,0);
  45. }
  46. }

Report this snippet  

You need to login to post a comment.