Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. /*
  2.  
  3. Algoritmi de sortare
  4.  
  5. [1] Bubble Sort
  6. [2] Select Sort
  7. [3] Counter Sort
  8. [4] Shell Sort
  9. ----------------------
  10. */
  11. #include<iostream>
  12. #include<cstdlib>
  13. #include<time.h>
  14.  
  15. using namespace std;
  16.  
  17. int n;
  18. int V[1000];
  19.  
  20. int init_data()
  21. {
  22. srand(time(0)); // seed randomize " pornim motorul " !
  23. do { n=rand()%10; } while (n==0);
  24. for(int i=1;i<=n;i++) V[i]=rand()%100;
  25. }
  26.  
  27. int print_data()
  28. {
  29. cout<<" Elements : "<<n<<endl;
  30. for(int i=1;i<=n;i++) cout<<V[i]<<" ";
  31. }
  32.  
  33. int sort_data()
  34. {
  35. /*
  36. Heap Sort :
  37. Lucreaza reorganizand vectorul ca heap de n-1 ori
  38.  
  39. pas i=1 de la 1 la n organizez heap
  40. pas i=2 de la 2 la n organizez heap
  41. ...
  42. pas n-1 de la n-1 la n organizez heap
  43.  
  44. Unde : Prin heap intelegem o structura de tip arbore binar
  45. in care fiecare nod are un nume si detine o informatie astfel incat
  46.  
  47. [1] Daca nodul i are descendenti acestia se numesc 2*i si 2*i+1
  48. [2] Informatia nodului i este >= informatia descendentilor lui
  49.  
  50. 542 233 144
  51.  
  52. Exemplu Fie n=5 si heapul
  53.  
  54. 1 [ 543 ]
  55. / \
  56. 2 [233] 3 [144]
  57. / \
  58. 4[104] 5 [73]
  59.  
  60. Acest heap se poate reprezenta sub forma de vector
  61.  
  62. 788
  63.  
  64. --------------------
  65. | 788 | 543 | 144 | 233
  66. ---------------------------------
  67. 1 2 3 4
  68.  
  69. Obs : Intr-un heap ( root oriented ) informatia maxima este in radacina
  70.  
  71. Constructia unui heap :
  72.  
  73. 1. Pornim cu heapul singleton
  74.  
  75. La un pas arbitrar k inseram un al k-lea element in heap
  76.  
  77. Element k are tata |k/2| !!!
  78.  
  79. --------------------
  80. | 543 | 233 |
  81. ---------------------
  82. 1 2
  83.  
  84.  
  85. */
  86. }
  87.  
  88. int main()
  89. {
  90. init_data();
  91. cout<<"Initial ";
  92. print_data();
  93. sort_data();
  94. cout<<endl<<"Final ";
  95. print_data();
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement