Advertisement
nima_fl

Untitled

Mar 31st, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #include <iomanip>
  6. #include <string>
  7.  
  8. using namespace std;
  9. unsigned long long int c = 0;
  10. unsigned long long int m = 0;
  11.  
  12. int compare(const void*, const void*);
  13. void printArray(int[], int);
  14. void inputArray(int[], int);
  15. void put_data(int*, int);
  16. int ShellSort(int[], int);
  17. double timer(int*, int);
  18.  
  19.  
  20. int main() {
  21.     srand(time(NULL));
  22.     const int n = 10;
  23.     int a[n];
  24.     cout << "Исходный массив " << n << " элементов: "<<endl;
  25.     inputArray(a,n);
  26.     cout <<endl<< "----- Тестовый прогон -----" << endl;
  27.     ShellSort(a, n);
  28.     cout << endl<<"Результат тестового сортировки шелла: "<<endl;
  29.     printArray(a, n);
  30.     cout << endl;
  31.     cout << endl << "--------------- Контрольный Прогон ---------------" << endl;
  32.     c = 0;
  33.     m = 0;
  34.     for (int i = 60000; i <= 100000; i += 10000) {
  35.         int* work_data = new int[i];
  36.         double time;
  37.         put_data(work_data, i);
  38.         cout << "---------- " << i  << " элементов ----------" << endl;
  39.         time = timer(work_data, i);
  40.         cout << "Средний   случай для сортировки шелла [c+m]: " << "C: "  << c << setw(12-to_string(c).length())<< "  M: " << m << " C+M: " << (c + m) << setw(12 - to_string(c + m).length()) << "  время выполнения: " << time << endl;
  41.         c = 0;
  42.         m = 0;
  43.         time = timer(work_data, i);
  44.         cout << "Наилучший случай для сортировки шелла [c+m]: " << "C: " << c << setw(12-to_string(c).length()) << "  M: " << m << "       C+M: " << (c + m) << right << setw(10) << "  время выполнения: " << time << endl;
  45.         c = 0;
  46.         m = 0;
  47.         qsort(work_data, i, sizeof(int), compare);
  48.         time = timer(work_data, i);
  49.         cout << "Наихудший случай для сортировки шелла [c+m]: " << "C: " << c << setw(12 - to_string(c).length()) << "  M: " << m << "  C+M: " << (c + m) << setw(12 - to_string(c + m).length()) << "  время выполнения: " << time << endl;
  50.         delete[]work_data;
  51.    
  52.     }
  53.     _getch();
  54. }
  55. int compare(const void* a, const void* b)
  56. {
  57.     return (*(int*)b - *(int*)a);
  58. }
  59. void printArray(int data[], int size)
  60. {
  61.     for (int i = 0; i < size; ++i) {
  62.         cout << data[i] << " ";
  63.     }
  64. }
  65. void inputArray(int inputData[], int size)
  66. {
  67.     for (int i = 0; i < size; i++)
  68.     {
  69.         int x; cin >> x;
  70.         inputData[i] = x;
  71.     }
  72. }
  73. void put_data(int* data, int size)
  74. {
  75.     for (int i = 0; i < size; i++) {
  76.         data[i] = rand();
  77.     }
  78. }
  79. int ShellSort(int arr[], int n)
  80. {
  81.     for (int gap = n / 2; gap > 0; gap /= 2)
  82.     {
  83.         for (int i = gap; i < n; i++)
  84.         {
  85.             int temp = arr[i];
  86.  
  87.             int j;
  88.             c++;
  89.             for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
  90.             {
  91.                 arr[j] = arr[j - gap];
  92.                 m++;
  93.             }
  94.  
  95.             arr[j] = temp;
  96.         }
  97.     }
  98.     return 0;
  99. }
  100. double timer(int* data, int size)
  101. {
  102.     clock_t start, end;
  103.     double seconds;
  104.     start = clock();
  105.     c = 0;
  106.     m = 0;
  107.     ShellSort(data, size);
  108.     end = clock();
  109.     seconds = (double)(end - start) / CLOCKS_PER_SEC;
  110.     return seconds;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement