Advertisement
patryk

Untitled

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