Posted By


sargaxon on 01/19/15

Tagged


Statistics


Viewed 83 times
Favorited by 0 user(s)

sortiranje hrpe


/ Published in: C++
Save to your folder(s)

strukture podataka


Copy this code and paste it in your HTML
  1. #include <iostream.h>
  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. void main(){
  9. int i;
  10. int n=10;
  11. int a[10];
  12. cout<<endl<<"Ucitavanje:"<<endl;
  13. for(i=0;i<n;i++){
  14. cout<<"Unesi "<<i+1<<". broj: ";
  15. cin>>a[i];
  16. }
  17.  
  18. cout<<endl<<"Ucitano je polje:"<<endl;
  19. for(i=0;i<n;i++)
  20. cout<<a[i];
  21. cout<<endl;
  22.  
  23. HeapSort(a, n);
  24.  
  25. cout<<"Sortirano polje:"<<endl;
  26. for(i=0;i<n;i++)
  27. cout<<a[i];
  28. }
  29.  
  30. void Podesi(int a[], int n, int i){
  31. int j,t;
  32.  
  33. j = 2 * i + 1;
  34. t = a[i];
  35.  
  36. while(j<n)
  37. {
  38. if(j < (n-1) && a[j] < a[j+1])j++;
  39. if(t >= a[j])break;
  40.  
  41. a[i] = a[j];
  42. i = j;
  43. j = 2*j+1;
  44. }
  45.  
  46. a[i] = t;
  47. }
  48.  
  49. void StvoriGomilu(int a[], int n){
  50. int i;
  51. for(i=(n-2)/2;i>=0;i--)Podesi(a,n,i);
  52. }
  53.  
  54. void HeapSort(int a[], int n){
  55. int i,t;
  56.  
  57. StvoriGomilu(a,n);
  58. for(i=n;i>=2;i--)
  59. {
  60. t = a[i-1];
  61. a[i-1] = a[0];
  62. a[0] = t;
  63.  
  64. Podesi(a,i-1,0);
  65. }
  66. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.