Advertisement
Adytzu04

l7p1 SDA

May 31st, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. //h
  2. #ifndef             HEADER_H
  3. #define             HEADER_H
  4.  
  5. #include<iostream>
  6. #include<conio.h>
  7. #include<ctime>
  8. #include<time.h>
  9.  
  10. using namespace std;
  11.  
  12. #define max 10000001
  13.  
  14.  
  15. void heapsort(int N,double v[]);
  16. void Remove(int &N,double v[]);
  17. void heapgen2(int N,double v[]);
  18. void retro(int &N, double v[], int p);
  19. void heapgen1(int N,double v[]);
  20. void insert(int &N, double v[],double a);
  21. void heapsort2(int N,double v[]);
  22.  
  23.  
  24. #endif
  25.  
  26. //cpp
  27.  
  28. #include "header.h"
  29. extern double v[max];
  30. void insert(int &N, double v[],double a)
  31. {
  32.     v[++N]=a;
  33.     int f,p;
  34.     double aux;
  35.     for(f=N;f>1;)
  36.     {
  37.         p=f>>1;//inseamna impartirea fiului la 2. este mai rapida ca impartirea normala
  38.         if(v[f]>v[p])
  39.         { aux=v[f];
  40.         v[f]=v[p];
  41.         v[p]=aux;
  42.         f=p;
  43.         }
  44.         else
  45.         {
  46.             f=0;
  47.         }
  48.     }
  49. }
  50.  
  51.  
  52. void heapgen1(int N,double v[])   //generare heap folosind insertii;
  53. {
  54.     int i;
  55.     for(i=1;i<N;)
  56.         insert(i,v,v[i+1]);
  57. }
  58.  
  59. void retro(int &N, double v[], int p)
  60. {
  61.     int f;
  62.     double aux;
  63.     while(p<<1<=N)
  64.     {
  65.         f=p<<1;
  66.         if((f+1<=N)&&(v[f+1]>v[f]))
  67.             ++f;
  68.         if(v[p]<v[f])
  69.         {
  70.             aux=v[p];
  71.             v[p]=v[f];
  72.             v[f]=aux;
  73.             p=f;
  74.         }
  75.         else
  76.             p=N;
  77.     }
  78. }
  79.  
  80.  
  81. void heapgen2(int N,double v[])  //generare heap folosin a doua metoda;
  82. {
  83.     int i;
  84.     for(i=N>>1;i>1;i--)
  85.         retro(N,v,i);
  86. }
  87.  
  88.  
  89. void Remove(int &N,double v[])
  90. {
  91.     double aux=v[1];
  92.     v[1]=v[N];
  93.     v[N--]=aux;
  94.     retro(N,v,1);
  95. }
  96.  
  97. void heapsort(int N,double v[])
  98. {
  99.     heapgen2(N,v);
  100.     while(N>1)
  101.     {
  102.         Remove(N,v);
  103.     }
  104. }
  105.  
  106. void heapsort2(int N,double v[])
  107. {
  108.     heapgen1(N,v);
  109.     while(N>1)
  110.     {
  111.         Remove(N,v);
  112.     }
  113. }
  114.  
  115. //main
  116.  
  117. #include "header.h"
  118.  
  119. double v[max];
  120. int main()
  121. {
  122.     //double start,final;
  123.     clock_t start,final,t,f;
  124.     int n = max-1;
  125.     for(int i=1;i<n;i++)
  126.     {
  127.         v[i]=i;
  128.     }
  129.     cout<<"values entered"<<endl;
  130.    
  131.     //system("PAUSE");
  132.    
  133.     cout<<"Clock started"<<endl;
  134.  
  135.     start=clock();
  136.     heapsort(n,v);
  137.     final=clock();
  138.     cout<<"heapsort2"<<endl;
  139.     cout<<double(final-start)/CLOCKS_PER_SEC<<endl;
  140.  
  141.     t=clock();
  142.     heapsort2(n,v);
  143.     f=clock();
  144.  
  145.     cout<<"Starting Heapsort 1..."<<endl;
  146.     cout<<double(f-t)/CLOCKS_PER_SEC<<endl;
  147.  
  148.     system("PAUSE");
  149.  
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement