Posted By

8ivana8 on 01/19/15


Tagged

Strukture podataka binarno stablo sortiranje hrpa


Versions (?)

algoritam sortiranja_hrpa


 / Published in: C++
 

Implementacija algoritma sortiranja pomoću hrpe iz kolegija Strukture podataka.

  1. void InsertH(int x, array_tree *T){
  2. int counter = 2;
  3. if(T->array[9999].used){
  4. cout << "Hrpa je puna! Pokrenite program iznova i pokusajte ponovno." << endl;
  5. return;
  6. }
  7.  
  8. while(T->array[counter].used)
  9. counter++;
  10.  
  11. T->array[counter].used = 1;
  12.  
  13. while(counter > 1 && x < T->array[counter / 2].label){
  14. T->array[counter].label = T->array[counter / 2].label;
  15. counter /= 2;
  16. }
  17.  
  18. T->array[counter].label = x;
  19. }
  20.  
  21. void SortH(array_tree *T){
  22. int counter = 1;
  23. cout << T->array[counter].label << " ";
  24.  
  25. if(T->array[counter+1].used == 0)
  26. return;
  27.  
  28. while(T->array[counter + 1].used)
  29. counter++;
  30.  
  31. T->array[1].label = T->array[counter].label;
  32. T->array[counter].label = -1;
  33. T->array[counter].used = 0;
  34.  
  35. counter = 1;
  36. int b = 2;
  37. while(T->array[b].used && ((T->array[counter].label > T->array[b].label) || (T->array[b+1].used && (T->array[counter].label > T->array[b+1].label)))){
  38. if(T->array[b].used && T->array[b+1].used)
  39. if(T->array[b].label > T->array[b+1].label)
  40. b++;
  41.  
  42. int tmp = T->array[counter].label;
  43. T->array[counter].label = T->array[b].label;
  44. T->array[b].label = tmp;
  45.  
  46. counter = b;
  47. b *= 2;
  48. }
  49.  
  50. SortH(T);
  51. }

Report this snippet  

You need to login to post a comment.