Posted By

igotepava on 01/18/15


Tagged

data array tree structures binary sorting heap


Versions (?)

hrpa.h


 / Published in: C++
 

Implementacija funkcija za heap sort. Zadatak 4 iz kolegija Strukture podataka

  1. void insert_h(int x, tree *T){
  2. if(T->elements[9999].used){
  3. cout << "Hrpa je puna!\n";
  4. return;
  5. }
  6. int j = 2;
  7. while(T->elements[j].used) j++;
  8. T->elements[j].used = 1;
  9. while(j>1 && x<T->elements[j/2].label){
  10. T->elements[j].label = T->elements[j/2].label;
  11. j /= 2;
  12. }
  13. T->elements[j].label = x;
  14.  
  15. }
  16.  
  17. void sort_h(tree *T){
  18. int j = 1;
  19. cout << T->elements[j].label << " ";
  20. if(!(T->elements[j+1].used)) return;
  21.  
  22. while(T->elements[j+1].used) j++;
  23. T->elements[1].label = T->elements[j].label;
  24. T->elements[j].label = -1;
  25. T->elements[j].used = 0;
  26.  
  27. j = 1;
  28. int b = 2;
  29. while(T->elements[b].used && ((T->elements[j].label > T->elements[b].label) || (T->elements[b+1].used && (T->elements[j].label > T->elements[b+1].label)))){
  30. if(T->elements[b].used && T->elements[b+1].used)
  31. if(T->elements[b].label > T->elements[b+1].label) b++;
  32.  
  33. int tmp = T->elements[j].label;
  34. T->elements[j].label = T->elements[b].label;
  35. T->elements[b].label = tmp;
  36.  
  37. j = b;
  38. b *= 2;
  39. }
  40. sort_h(T);
  41. }

Report this snippet  

You need to login to post a comment.