Advertisement
patryk

Untitled

Mar 19th, 2014
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<windows.h>
  4.  
  5. #define N 100
  6.  
  7. int a[N], i, j, x, l, p;
  8.  
  9. //----------------------------------------
  10. void przesiewanie(int l, int  p){
  11.     i = l;
  12.     j = 2*i;
  13.     x = a[i];
  14.     while(j<=p){
  15.         if(j<p){
  16.             if(a[j] < a[j+1]){
  17.                 j = j+1;
  18.             }
  19.         }
  20.         if(x>=a[j]){
  21.             goto me;
  22.         }
  23.         a[i] = a[j];
  24.         i=j;
  25.         j=2*i;
  26.     }
  27.     me:
  28.         a[i] = x;
  29. }
  30.  
  31.  
  32. void heapsort(){
  33.  
  34.     l = (N/2)+1;
  35.     p = N-1;
  36.     while(l>0){
  37.         l = l-1;
  38.         przesiewanie(l, p);
  39.     }
  40.  
  41.     while(p>0){
  42.         x = a[0];
  43.         a[0] = a[p];
  44.         a[p] = x;
  45.         p = p-1;
  46.         przesiewanie(l, p);
  47.     }
  48. }
  49. //--------------------------------------
  50.  
  51. double GetTime()
  52. {
  53.   long long f,t;
  54.   QueryPerformanceFrequency((PLARGE_INTEGER)&f);
  55.   QueryPerformanceCounter((PLARGE_INTEGER)&t);
  56.   return (double)t/(double)f;
  57. }
  58.  
  59.  
  60. int main(){
  61.     //------LOSOWY-------------------
  62.     srand(time(NULL));
  63.     for(i=0; i<N; i++) a[i] = rand();
  64.     double one=GetTime();
  65.     heapsort();
  66.     double two=GetTime();
  67.     printf("Losowy: \t%lf\n", (double)(two-one));
  68.     //--------------------------------
  69.  
  70.  
  71.     //-------CIAG-STALY---------------
  72.     for(i=0; i<N; i++) a[i] = 3;
  73.     one=GetTime();
  74.     heapsort();
  75.     two=GetTime();
  76.     printf("Ciag staly: \t%lf\n", (double)(two-one));
  77.     //--------------------------------
  78.  
  79.  
  80.     //----------CIAG-ROSNACY----------
  81.     for(i=0; i<N; i++) a[i] = i+1;
  82.     one=GetTime();
  83.     heapsort();
  84.     two=GetTime();
  85.     printf("Ciag rosnacy: \t%lf\n", (double)(two-one));
  86.     //--------------------------------
  87.  
  88.  
  89.     //----------CIAG-MALEJACY----------
  90.     j=8;
  91.     for(i=0; i<N; i++){
  92.       a[i] = j;
  93.       j--;
  94.     }
  95.     one=GetTime();
  96.     heapsort();
  97.     two=GetTime();
  98.     printf("%d\n\n", a[0]);
  99.     for(i=0; i<N; i++) printf("%d\t", a[i]);
  100.     printf("Ciag malejacy: \t%lf\n", (double)(two-one));
  101.     //---------------------------------
  102.  
  103.  
  104.     //for(i=0; i<N-1; i++) if(a[i] > a[i+1]){ printf("Nie jest posortowany"); break;}
  105.     //for(i=0; i<N; i++) printf("[%d]: %d\t", i, a[i]);
  106.  
  107.     getch();
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement