Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3.                               Online C++ Compiler.
  4.                Code, Compile, Run and Debug C++ program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <iostream>
  10. #include <cstdlib>
  11. #include <cstdio>
  12. #include <ctime>
  13. #include <cmath>
  14.  
  15. using namespace std;
  16.  
  17. int count_sw = 0;
  18.  
  19.  
  20. void q_sort(int *tab, int left, int right)
  21. {
  22.  
  23.     int i = left - 1, j = right + 1,
  24.     pivot = tab[left];
  25.     while(1)
  26.     {
  27.  
  28.         while(pivot>tab[++i]);
  29.         while(pivot<tab[--j]);
  30.         if( i <= j)
  31.         {
  32.             swap(tab[i],tab[j]);
  33.             count_sw++;
  34.  
  35.         }
  36.         else
  37.             break;
  38.     }
  39.     if(j > left)
  40.     q_sort(tab, left, j);
  41.     if(i < right)
  42.     q_sort(tab, i, right);
  43. }
  44.  
  45.  
  46.  
  47. int main()
  48. {
  49.    
  50.     srand( time( NULL ) );
  51.     int size =10;
  52.     int *tab, n[size];
  53.     clock_t start, stop;
  54.  
  55.    
  56.     double times [size];
  57.     for(int c = 0 ; c < size; c++ )
  58.     {
  59.         n [c] = (c+1) * 1000;
  60.         count_sw = 0;
  61.         tab = new int [n[c]];
  62.         for(int i=0;i<n[c];i++)
  63.             tab[i] = rand()%n[c];//
  64.        
  65.         start = clock();
  66.         q_sort(tab,0, n[c]-1);
  67.         stop = clock();
  68.         cout <<"size "<<n[c]<<"   changes-"<<count_sw<<'\n';
  69.         for(int i=0;i<n[c];i++)
  70.             //cout<<tab[i]<<' ';//rand()%n[c];
  71.         //cout<<'\n';
  72.         count_sw =0;
  73.         times[c] = (double)(stop-start) / CLOCKS_PER_SEC;
  74.         delete[] tab;
  75.     }
  76.    
  77.     for(int c = 0 ; c < size; c++ )
  78.     {
  79.         cout<< /*n[c]<< "\n"<<*/times[c]<<'\n';
  80.     }
  81.  
  82.  
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement