Advertisement
TheMagnusRex

ЗАмерение времени

Dec 15th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. #include "time.h"
  3. #include "random"
  4. #include "fstream"
  5.  
  6. using namespace std;
  7. void qsort(int * a, int n){
  8.     int c = a[rand()%n];
  9.     int l=0;
  10.     int r = n-1;
  11.     do{
  12.         while(a[l]<c) l++;
  13.         while(a[r]>c) r--;
  14.         if (l<=r){
  15.             swap(a[l],a[r]);
  16.             l++;
  17.             r--;
  18.         }
  19.  
  20.     }
  21.     while (l<=r);
  22.     if (l<n){
  23.         qsort(a+l,n-l);
  24.     }
  25.     if (r>0){
  26.         qsort(a,r+1);
  27.     }
  28. }
  29.  
  30. int main()
  31. {
  32.     ofstream myfile;
  33.     int n;
  34.     cin>>n;
  35.     int *a = new int[n];
  36.     myfile.open ("example.txt");
  37.     for (int k=0;k<10;k++){
  38.         for (int x=0;x<n;x++){
  39.             a[x]=rand()%1000;
  40.         }
  41.         int t1=clock();
  42.         qsort(a,n);
  43.         int t2=clock();
  44.         myfile <<t2-t1<<"\n";
  45.     }
  46.     myfile.close();
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement