AmateurKid

Untitled

Dec 20th, 2021
879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <chrono>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8.  
  9.  
  10. void print_vec(vector<int> vec, int arr_size) {
  11.     int max_arr;
  12.     if (arr_size > 30) { //если количество символов больше 30, то сортируются все, но выводятся лишь первые 30 отсортированных
  13.         max_arr = 30;
  14.     }
  15.     else {
  16.         max_arr = arr_size;
  17.     }
  18.  
  19.     for (int i = 0; i < max_arr; i++)
  20.         cout << vec[i] << ' ';
  21. }
  22.  
  23.  
  24. void qsort(int b, int e, vector<int>& vec)
  25. {
  26.    
  27.     int l = b, r = e;
  28.     int piv = vec[(l + r) / 2];
  29.     while (l <= r)
  30.     {
  31.         while (vec[l] < piv)
  32.             l++;
  33.         while (vec[r] > piv)
  34.             r--;
  35.         if (l <= r)
  36.             swap(vec[l++], vec[r--]);
  37.     }
  38.     if (b < r)
  39.         qsort(b, r, vec);
  40.     if (e > l)
  41.         qsort(l, e, vec);
  42.    
  43. }
  44.  
  45.  
  46. int main()
  47. {
  48.     char answ;
  49.     do {
  50.         int arr_size;
  51.         char answ_input;
  52.         cout << "Enter array size>>";
  53.         cin >> arr_size;
  54.         int a;
  55.  
  56.         while (cin.fail() || arr_size < 1 || cin.peek() != '\n')
  57.         {
  58.             cout << "Error, invalid parameters \n";
  59.             cin.clear();
  60.             cin.ignore(10000, '\n');
  61.             cout << "Enter array size>>";
  62.             cin >> arr_size;
  63.         }
  64.  
  65.  
  66.  
  67.         vector <int> vec1;
  68.         vector <int> vec2;
  69.         vec1.reserve(arr_size);
  70.         vec1.reserve(arr_size);
  71.  
  72.  
  73.         cout << "How to fill vector?(1 - from keyboard, 2 - random numbers, 3 - random numbers sorted backwards)>>";
  74.         cin >> answ_input;
  75.         while (cin.fail() || cin.peek() != '\n' || (answ_input != '1' && answ_input != '2' && answ_input != '3'))
  76.         {
  77.             cin.clear();
  78.             cin.ignore(10000, '\n');
  79.             cout << "Error, invalid parameters \n";
  80.             cout << "How to fill vector?(1 - from keyboard, 2 - random numbers, 3 - random numbers sorted backwards>>";
  81.             cin >> answ_input;
  82.         }
  83.  
  84.         if (answ_input == '1') {
  85.             for (int i = 0; i < arr_size; i++) {
  86.                 cout << "Enter " << i + 1 << " element>>";
  87.                 cin >> a;
  88.                 while (cin.fail() || cin.peek() != '\n')
  89.                 {
  90.                     cin.clear();
  91.                     cin.ignore(10000, '\n');
  92.                     cout << "Error, invalid parameter \n";
  93.                     cin >> a;
  94.                 }
  95.                 vec1.push_back(a);
  96.                 vec2.push_back(a);
  97.                
  98.             }
  99.             cout << "Not sorted vector:" << endl;
  100.             print_vec(vec1, arr_size);
  101.             cout << endl;
  102.             auto start1 = chrono::high_resolution_clock::now();
  103.             sort(vec1.begin(), vec1.end());
  104.             auto end1 = chrono::high_resolution_clock::now();
  105.             chrono::duration <double> duration1 = end1 - start1;
  106.             cout << "Function sorted this vector in " << duration1.count() << " ms" << endl;
  107.  
  108.  
  109.             auto start2 = chrono::high_resolution_clock::now();
  110.             qsort(0, (arr_size - 1), vec2);
  111.             auto end2 = chrono::high_resolution_clock::now();
  112.             chrono::duration <double> duration2 = end2 - start2;
  113.             cout << "Algorithm sorted this vector in " << duration2.count() << " ms" << endl;
  114.  
  115.             cout << "This vector was sorted by function:" << endl;
  116.             print_vec(vec1, arr_size);
  117.             cout << endl;
  118.             cout << "This vector was sorted by algorithm:" << endl;
  119.             print_vec(vec2, arr_size);
  120.  
  121.             cout << endl;
  122.             if (duration1.count() - duration2.count() > 0)
  123.             {
  124.                 cout << "The function was faster by " << (duration1.count() / duration2.count()) * 1000 << " ms \n";
  125.             }
  126.             else
  127.             {
  128.                 cout << "The algorithm was faster by " << abs((duration1.count() / duration2.count())) * 1000 << " ms \n";
  129.             }
  130.  
  131.         }
  132.         else if (answ_input == '2') {
  133.             for (int i = 0; i < arr_size; i++) {
  134.                 a = rand() % 100;
  135.                 vec1.push_back(a);
  136.                 vec2.push_back(a);
  137.                
  138.             }
  139.             cout << "Not sorted vector:" << endl;
  140.             print_vec(vec1, arr_size);
  141.             cout << endl;
  142.             auto start1 = chrono::high_resolution_clock::now();
  143.             sort(vec1.begin(), vec1.end());
  144.             auto end1 = chrono::high_resolution_clock::now();
  145.             chrono::duration <double> duration1 = end1 - start1;
  146.             cout << "Function sorted this vector in " << duration1.count() << " ms" << endl;
  147.            
  148.  
  149.             auto start2 = chrono::high_resolution_clock::now();
  150.             qsort(0, (arr_size - 1), vec2);
  151.             auto end2 = chrono::high_resolution_clock::now();
  152.             chrono::duration <double> duration2 = end2 - start2;
  153.             cout << "Algorithm sorted this vector in " << duration2.count() << " ms" << endl;
  154.  
  155.             cout << "This vector was sorted by function:" << endl;
  156.             print_vec(vec1, arr_size);
  157.             cout << endl;
  158.             cout << "This vector was sorted by algorithm:" << endl;
  159.             print_vec(vec2, arr_size);
  160.  
  161.             cout << endl;
  162.             if (duration1.count() - duration2.count() > 0)
  163.             {
  164.                 cout << "The function was faster by " << (duration1.count() / duration2.count()) * 1000 << " ms \n";
  165.             }
  166.             else
  167.             {
  168.                 cout << "The algorithm was faster by " << abs((duration1.count() / duration2.count())) * 1000 << " ms \n";
  169.             }
  170.         }
  171.         else if (answ_input == '3') {
  172.             for (int i = 0; i < arr_size; i++) {
  173.                 a = rand() % 100;
  174.                 vec1.push_back(a);
  175.                 vec2.push_back(a);
  176.                
  177.             }
  178.             cout << "Not sorted vector:" << endl;
  179.             print_vec(vec1, arr_size);
  180.             cout << endl;
  181.  
  182.             auto start1 = chrono::high_resolution_clock::now();
  183.             sort(vec1.begin(), vec1.end(), greater<int>());
  184.            
  185.             auto end1 = chrono::high_resolution_clock::now();
  186.             chrono::duration <double> duration1 = end1 - start1;
  187.             cout << "Function sorted this vector in " << duration1.count() << " ms" << endl;
  188.  
  189.             auto start2 = chrono::high_resolution_clock::now();
  190.             qsort(0, (arr_size - 1), vec2);
  191.             reverse(vec2.begin(), vec2.end());
  192.             auto end2 = chrono::high_resolution_clock::now();
  193.             chrono::duration <double> duration2 = end2 - start2;
  194.             cout << "Algorithm sorted this vector in " << duration2.count() << " ms" << endl;
  195.  
  196.             cout << "This vector was sorted by function:" << endl;
  197.             print_vec(vec1, arr_size);
  198.             cout << endl;
  199.             cout << "This vector was sorted by algorithm:" << endl;
  200.             print_vec(vec2, arr_size);
  201.  
  202.             cout << endl;
  203.             if (duration1.count() - duration2.count() > 0)
  204.             {
  205.                 cout << "The function was faster by " << (duration1.count() / duration2.count()) * 1000 << " ms \n";
  206.             }
  207.             else
  208.             {
  209.                 cout << "The algorithm was faster by " << abs((duration1.count() / duration2.count())) * 1000 << " ms \n";
  210.             }
  211.         }
  212.  
  213.        
  214.  
  215.         vec1.clear();
  216.         vec2.clear();
  217.  
  218.         cout << "\nDo you want to repeat (y/n)>> ";
  219.         cin >> answ;
  220.         while (cin.fail() || cin.peek() != '\n' || (answ != 'n' && answ != 'N' && answ != 'Y' && answ != 'y'))
  221.         {
  222.             cin.clear();
  223.             cin.ignore(10000, '\n');
  224.             cout << "Error, invalid parameters \n";
  225.             cout << "\nDo you want to repeat (y/n)>> ";
  226.             cin >> answ;
  227.         }
  228.     } while ((answ == 'y') || (answ == 'Y'));
  229.     return 0;
  230. }
  231.  
  232.  
Advertisement
Add Comment
Please, Sign In to add comment