Advertisement
dimon-torchila

Untitled

Jan 4th, 2023
1,060
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include "lab1.h"
  2. #include "lab2.h"
  3. #include "lab3.h"
  4.  
  5. using namespace std;
  6.  
  7.  
  8.  
  9.  
  10. void sort(lab1::Queue<int>& q){
  11.     q.N_op+=3;
  12.     for(int i = 0; i < q.size(); ++i){
  13.         q.N_op+= 12;
  14.         int mn = INT_MAX;
  15.         int first = 0, temp = 0;
  16.         bool swap = true;
  17.         for(int j = 0; j < i; ++j){
  18.             q.N_op++;
  19.             temp = q.front();
  20.             q.pop();
  21.             q.push(temp);
  22.         }
  23.         first = q.front();
  24.         for(int j = i; j < q.size(); ++j){
  25.             temp = q.front();
  26.             mn = min(q.front(), mn);
  27.             q.pop();
  28.             q.push(temp);
  29.             q.N_op+=3;
  30.         }
  31.         for(int j = 0; j < i; ++j){
  32.             q.N_op++;
  33.             temp = q.front();
  34.             q.pop();
  35.             q.push(temp);
  36.         }
  37.         for(int j = i; j < q.size(); ++j){
  38.             q.N_op+=5;
  39.             temp = q.front();
  40.             q.pop();
  41.             if(temp == mn && swap) {
  42.                 q.push(first);
  43.                 swap = false;
  44.             }
  45.             else if(i == j) {
  46.                 q.push(mn);
  47.             }
  48.             else
  49.                 q.push(temp);
  50.         }
  51.     }
  52. }
  53.  
  54.  
  55. int main(){
  56.     for (int a = 3000; a <= 4000; a += 200) {
  57.         size_t n = 0;
  58.         lab1::Queue<int> queue(a);
  59.         srand(time(NULL));
  60.         ::clock_t start = ::clock();
  61.         for(int i = 0; i < a; ++i){
  62.             queue.push(::rand() % (10 * a));
  63.         }
  64.         cout << "init of " << a << " elements takes " <<
  65.             1000 * (::clock() - start) / CLOCKS_PER_SEC << '\n';
  66.         n += queue.N_op;
  67.         start = ::clock();
  68.         sort(queue);
  69.         cout << "sort of " << a << " elements takes " << 1000 * (::clock() - start) / CLOCKS_PER_SEC << " millisec\n";
  70.  
  71.         cout << "takes " << n << " N_op\n\n";
  72.     }
  73.     return 0;
  74. }
  75.  
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement