Norvager

Sort

Sep 10th, 2021 (edited)
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.82 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <chrono>
  4. #include <ctime>
  5. using namespace std;
  6. using namespace chrono;
  7. int countSwapped, countCheck;
  8. void reverseCombSort(double* arr, int size) {
  9.     int step = size;
  10.     bool swapped = true;
  11.     while (step > 1 || swapped) {
  12.         if (step > 1)
  13.             step /= 1.2473309;
  14.         swapped = false;
  15.         for (int i = 0; i + step < size; ++i)
  16.             if (arr[i + step] > arr[i]) {
  17.                 double tmp = arr[i + step];
  18.                 arr[i + step] = arr[i];
  19.                 arr[i] = tmp;
  20.                 swapped = true;
  21.             }
  22.     }
  23. }
  24.  
  25. void output(double* arr, int size) {
  26.     for (int i = 0; i < size - 1; i++) {
  27.         cout << arr[i] << " ";
  28.     }
  29.     cout << arr[size - 1] << endl << endl;
  30. }
  31.  
  32. void combSort(double* arr, int size) {
  33.     int step = size;
  34.     double tmp;
  35.     bool swapped = true;
  36.     while (step > 1 || swapped) {
  37.         if (step > 1)
  38.             step /= 1.2473309;
  39.         swapped = false;
  40.         for (int i = 0; i + step < size; ++i) {
  41.             countCheck++;
  42.             if (arr[i + step] < arr[i]) {
  43.                 countSwapped++;
  44.                 tmp = arr[i + step];
  45.                 arr[i + step] = arr[i];
  46.                 arr[i] = tmp;
  47.                 swapped = true;
  48.             }
  49.         }
  50.     }
  51. }
  52.  
  53. void quickSort(double* arr, int size) {
  54.     double tmp;
  55.     int i = 0;
  56.     int j = size - 1;      
  57.     double mid = arr[size / 2];
  58.     do {
  59.         while (arr[i] < mid) {
  60.             i++;
  61.         }
  62.         while (arr[j] > mid) {
  63.             j--;
  64.         }
  65.         countCheck++;
  66.         if (i <= j) {
  67.             countSwapped++;
  68.             tmp = arr[i];
  69.             arr[i] = arr[j];
  70.             arr[j] = tmp;
  71.             i++;
  72.             j--;
  73.         }
  74.     } while (i <= j);
  75.     if (j > 0) {
  76.         quickSort(arr, j + 1);
  77.     }
  78.     if (i < size) {
  79.         quickSort(&arr[i], size - i);
  80.     }
  81. }
  82.  
  83. void ShellSort(double* arr, int size) {
  84.     int step = size / 2;
  85.     double tmp;
  86.     while (step > 0)
  87.     {
  88.         for (int i = 0; i < size - step; i++)
  89.         {
  90.             for (int j = i; j >= 0; j--) {
  91.                 countCheck++;
  92.                 if (arr[j] > arr[j + step]) {
  93.                     countSwapped++;
  94.                     tmp = arr[j];
  95.                     arr[j] = arr[j + step];
  96.                     arr[j + step] = tmp;
  97.                 }
  98.                 else {
  99.                     break;
  100.                 }
  101.             }
  102.         }
  103.         step /= 2;
  104.     }
  105. }
  106.  
  107. void createrArr(double* arr, int n) {
  108.     for (int i = 0; i < n; i++) {
  109.         arr[i] = (double)rand() / RAND_MAX * 50;
  110.     }
  111. }
  112.  
  113. void contrastSort(int n, int choose) {
  114.     time_point<high_resolution_clock> start, end;
  115.     double* mas_1 = new double[n];
  116.     createrArr(mas_1, n);
  117.     if (1 == choose) {
  118.         combSort(mas_1, n);
  119.         cout << "Best" << endl;
  120.     } else if (2 == choose) {
  121.         reverseCombSort(mas_1, n);
  122.         cout << "Bed" << endl;
  123.     }
  124.     double* mas_2 = new double[n];
  125.     double* mas_3 = new double[n];
  126.     memcpy(mas_2, mas_1, sizeof(double) * n);
  127.     memcpy(mas_3, mas_1, sizeof(double) * n);
  128.     countSwapped = 0;
  129.     countCheck = 0;
  130.     start = high_resolution_clock::now();
  131.     combSort(mas_1, n);
  132.     end = high_resolution_clock::now();
  133.     duration<int64_t, nano> dur_ns = (end - start);
  134.     unsigned long long elapsed_seconds = dur_ns.count();
  135.     cout << "comb: " << elapsed_seconds << " || swap: " << countSwapped << " || check: " << countCheck << endl;
  136.  
  137.     countSwapped = 0;
  138.     countCheck = 0;
  139.     start = high_resolution_clock::now();
  140.     quickSort(mas_2, n);
  141.     end = high_resolution_clock::now();
  142.     dur_ns = (end - start);
  143.     elapsed_seconds = dur_ns.count();
  144.     cout << "quick: " << elapsed_seconds << " || swap: " << countSwapped << " || check: " << countCheck << endl;
  145.  
  146.     countSwapped = 0;
  147.     countCheck = 0;
  148.     start = high_resolution_clock::now();
  149.     ShellSort(mas_3, n);
  150.     end = high_resolution_clock::now();
  151.     dur_ns = (end - start);
  152.     elapsed_seconds = dur_ns.count();
  153.     cout << "Shell: " << elapsed_seconds << " || swap: " << countSwapped << " || check: " << countCheck << endl;
  154.  
  155.     delete mas_1, mas_2, mas_3;
  156. }
  157.  
  158. int main() {
  159.     freopen("out.txt", "w", stdout);
  160.     int lenArr[23] = {1, 2, 3, 4, 5, 10, 15, 20, 25, 30, 40, 50, 75, 100, 150, 200, 250, 300, 400, 500, 600, 800, 1000};
  161.     for (int i = 0; i < 23; i++) {
  162.         cout << "For " << lenArr[i] << " element" << endl;
  163.         contrastSort(lenArr[i], 0);
  164.         cout << endl;
  165.     }
  166.     return 0;
  167. }
Add Comment
Please, Sign In to add comment