Vla_DOS

Untitled

Jun 28th, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include<iostream>
  2. #include <ctime>
  3. #include <string>
  4. #include <time.h>
  5. #include <chrono>
  6.  
  7. using namespace std;
  8.  
  9. void Print(int* arr, int size) {
  10.     for (int i = 0; i < size; i++) {
  11.         cout << arr[i] << "\t";
  12.     }
  13. }
  14. void Otsev(int* a, int& n)
  15. {
  16.     int j = 0;
  17.     for (int i = 0; i < n; ++i)
  18.         if (a[i] % 2 == 0)
  19.             a[j++] = a[i];
  20.     n = j;
  21. }
  22. void sortByInserts(int* arr, int N) {
  23.     int buff = 0;
  24.     int i, j;
  25.  
  26.     for (i = 1; i < N; i++)
  27.     {
  28.         buff = arr[i];
  29.         for (j = i - 1; j >= 0 && arr[j] > buff; j--)
  30.             arr[j + 1] = arr[j];
  31.  
  32.         arr[j + 1] = buff;
  33.     }
  34. }
  35. int main()
  36. {
  37.     srand((unsigned)time(0));
  38.     setlocale(0, "");
  39.  
  40.     int size = 0;
  41.     cout << "Введiть розмiр масиву: ";
  42.     cin >> size;
  43.     int* arr = new int[size];
  44.     int elementId = -1;
  45.     for (int i = 0; i < size; i++) {
  46.         arr[i] = rand() % 27 - 90;
  47.     }
  48.     int index = 0;
  49.     Print(arr, size);
  50.     cout << endl;
  51.     auto start1 = chrono::high_resolution_clock::now();
  52.     for (int i = 4; i < size; i += 5) {
  53.         arr[i] = 0;
  54.     }
  55.     auto finish1 = chrono::high_resolution_clock::now();
  56.     cout << "Час замiни на 0: " << chrono::duration_cast<chrono::duration<double>>(finish1 - start1).count() << " секунд\n\n";
  57.  
  58.     Print(arr, size);
  59.     auto start2 = chrono::high_resolution_clock::now();
  60.     Otsev(arr, size);
  61.     auto finish2 = chrono::high_resolution_clock::now();
  62.     cout << "\nЧас вилучення непарних елементiв: " << chrono::duration_cast<chrono::duration<double>>(finish2 - start2).count() << " секунд\n\n";
  63.     cout << endl;
  64.  
  65.     Print(arr, size);
  66.     cout << endl;
  67.     auto start3 = chrono::high_resolution_clock::now();
  68.     sortByInserts(arr, size);
  69.     auto finish3 = chrono::high_resolution_clock::now();
  70.     cout << "Час сортування: " << chrono::duration_cast<chrono::duration<double>>(finish3 - start3).count() << " секунд\n\n";
  71.     Print(arr, size);
  72.     cout << endl;
  73.  
  74.  
  75.     delete[] arr;
  76.     system("pause");
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment