Advertisement
dimon-torchila

Untitled

Jan 7th, 2023
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2.  
  3. using namespace std;
  4.  
  5.  
  6.     class Queue{
  7.     public:
  8.         unsigned long long N_op = 0;
  9.         Queue(int size) : length(size), arr(new int[size]), begin(0), count(0), end(-1){ N_op+=7; }
  10.         ~Queue(){ delete []arr; N_op++;}
  11.         void push(int el){
  12.             if(isFull())
  13.                 resize(length + length / 10);
  14.             ++end%=length;
  15.             arr[end] = el;
  16.             count++;
  17.             N_op+=8;
  18.         }
  19.         void pop(){
  20.             if(!isEmpty()){
  21.                 arr[begin] = INT_MIN;
  22.                 ++begin%=length;
  23.                 count--;
  24.             } else {
  25.                 throw runtime_error("going beyond the boundaries of the array");
  26.             }
  27.             N_op+=8;
  28.         }
  29.         int front(){
  30.             if(!isEmpty())
  31.                 return arr[begin];
  32.             else throw runtime_error("going beyond the boundaries of the array");
  33.             N_op+=5;
  34.         }
  35.         void resize(int new_size){
  36.             int *arr2 = arr;
  37.             arr = new int[new_size];
  38.             for(int i = 0; i < length; ++i){
  39.                 arr[i] = arr2[(i + begin) % length];
  40.             }
  41.             begin = 0;
  42.             end = length - 1;
  43.             length = new_size;
  44.             delete []arr2;
  45.             N_op+=4*length + 13;
  46.         }
  47.         bool isEmpty(){ return size() == 0; N_op+=2; }
  48.         bool isFull() { return size() == count; N_op+=2; }
  49.         int size() { return length; N_op++; }
  50.  
  51.  
  52.         void print(){
  53.             for(int i = begin; i < begin + count; ++i)
  54.                 cout << arr[i%length] << ' ';
  55.             cout << endl;
  56.         }
  57.  
  58.  
  59.  
  60.     private:
  61.         int *arr;
  62.         int length;
  63.         int begin;
  64.         int end;
  65.         int count;
  66.     };
  67. void sort(Queue& q){
  68.     q.N_op+=3;
  69.     for(int i = 0; i < q.size(); ++i){
  70.         q.N_op+= 12;
  71.         int mn = INT_MAX;
  72.         int first = 0, temp = 0;
  73.         bool swap = true;
  74.         for(int j = 0; j < i; ++j){
  75.             q.N_op++;
  76.             temp = q.front();
  77.             q.pop();
  78.             q.push(temp);
  79.         }
  80.         first = q.front();
  81.         for(int j = i; j < q.size(); ++j){
  82.             temp = q.front();
  83.             mn = min(q.front(), mn);
  84.             q.pop();
  85.             q.push(temp);
  86.             q.N_op+=3;
  87.         }
  88.         for(int j = 0; j < i; ++j){
  89.             q.N_op++;
  90.             temp = q.front();
  91.             q.pop();
  92.             q.push(temp);
  93.         }
  94.         for(int j = i; j < q.size(); ++j){
  95.             q.N_op+=5;
  96.             temp = q.front();
  97.             q.pop();
  98.             if(temp == mn && swap) {
  99.                 q.push(first);
  100.                 swap = false;
  101.             }
  102.             else if(i == j) {
  103.                 q.push(mn);
  104.             }
  105.             else
  106.                 q.push(temp);
  107.         }
  108.     }
  109. }
  110.  
  111. int main(){
  112.     for (int a = 5000; a <= 10000; a += 500) {
  113.         size_t n = 0;
  114.         Queue queue(a);
  115.         srand(time(NULL));
  116.         ::clock_t start = ::clock();
  117.         for(int i = 0; i < a; ++i)
  118.             queue.push(::rand() % (5 * a));
  119.         cout << "init of " << a << " elements takes " <<
  120.              (::clock() - start) * 1000 /CLOCKS_PER_SEC << " millisec\n";
  121.         start = ::clock();
  122.         sort(queue);
  123.         cout << "sort of " << a << " elements takes " <<
  124.              (::clock() - start) * 1000 /CLOCKS_PER_SEC << " millisec\n";
  125.         cout << "takes " << queue.N_op << " N_op\n\n";
  126.     }
  127.  
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement