Advertisement
Guest User

fuck u

a guest
Jun 18th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4.  
  5. int len = 50000000;
  6.  
  7. void ShellSort(int *A, int N, int a, int b) {
  8.     int j, t;
  9.    
  10.     for (int k = ((b - a) / b) * (N / 2); k > ((b - a - 1) / b) * (N / 2); k /= 2)
  11.         for (int i = k; i < N; i++) {
  12.             t = A[i];
  13.  
  14.             for (j = i; j >= k; j -= k)
  15.                 if (t < A[j - k])
  16.                     A[j] = A[j - k];
  17.                 else
  18.                     break;
  19.            
  20.             A[j] = t;
  21.         }
  22. }
  23.  
  24. void print(int*A) {
  25.     for (int i = 0; i < len; i++)
  26.         std::cout << " " << A[i];
  27.     std::cout << "\n\n";
  28. }
  29.  
  30. int main() {
  31.     int *A = new int[len];
  32.     int *B = new int[len];
  33.    
  34.     for (int i = 0; i < len; i++)
  35.         B[i] = A[i] = rand() % 1000;
  36.    
  37.     //std::cout << " Unsorted array:" << std::endl;
  38.     //print(A);
  39.    
  40.     int start_time = clock();
  41.     ShellSort(A, len, 0, 1);
  42.     int end_time = clock();
  43.  
  44.     std::cout << " Sorted by 1 thread:" << std::endl;
  45.     //print(A);
  46.     std::cout << "\n\t\tTime: " << end_time - start_time << std::endl;
  47.  
  48.     system("pause");
  49.     system("cls");
  50.  
  51.     //std::cout << " Unsorted array:" << std::endl;
  52.     //print(B);
  53.  
  54. OneMoreTry:
  55.     int amount;
  56.     std::cout << " Enter thread amount (max = " << len << " )" << std::endl;
  57.     std::cin >> amount;
  58.     if (amount > len || amount < 0) {
  59.         std::cout << " Error thread amount" << std::endl;
  60.         goto OneMoreTry;
  61.     }
  62.  
  63.     start_time = clock();
  64.     std::thread** stream = new std::thread*[amount];
  65.     for (int i = 0; i < amount; i++)
  66.         stream[i] = new std::thread(ShellSort, B, len, i, amount);
  67.     for (int i = 0; i < amount; i++)
  68.         stream[i]->join();
  69.     end_time = clock();
  70.  
  71.     std::cout << "Sorted by " << amount << " threads:" << std::endl;
  72.     //print(B);
  73.     std::cout << "Time: " << end_time - start_time << std::endl;
  74.    
  75.     system("pause");
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement