Advertisement
fatalryuu

Untitled

Oct 23rd, 2023
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <vector>
  5. #include <string>
  6. #include <algorithm>
  7. #include <chrono>
  8. #include <windows.h>
  9. #include <random>
  10.  
  11. std::wstring generateRandomText(int length) {
  12. std::wstring result;
  13. int newLength = length + rand() % 50;
  14. result.reserve(newLength);
  15.  
  16. for (int i = 0; i < newLength; ++i) {
  17. int unicode_code = 33 + rand() % 94;
  18. wchar_t symbol = static_cast<wchar_t>(unicode_code);
  19. result.push_back(symbol);
  20. }
  21.  
  22. return result;
  23. }
  24.  
  25. void quickSortRange(std::vector<std::wstring>& arr, int low, int high, bool ascending) {
  26. if (low < high) {
  27. int pivotIndex = low + (high - low) / 2;
  28. std::wstring pivot = arr[pivotIndex];
  29. int i = low;
  30. int j = high;
  31.  
  32. while (i <= j) {
  33. if (ascending == 1) {
  34. while (arr[i] < pivot) {
  35. i++;
  36. }
  37. while (arr[j] > pivot) {
  38. j--;
  39. }
  40. }
  41. else {
  42. while (arr[i] > pivot) {
  43. i++;
  44. }
  45. while (arr[j] < pivot) {
  46. j--;
  47. }
  48. }
  49.  
  50. if (i <= j) {
  51. std::swap(arr[i], arr[j]);
  52. i++;
  53. j--;
  54. }
  55. }
  56.  
  57. if (low < j) {
  58. quickSortRange(arr, low, j, ascending);
  59. }
  60. if (i < high) {
  61. quickSortRange(arr, i, high, ascending);
  62. }
  63. }
  64. }
  65.  
  66. struct ThreadData {
  67. std::vector<std::wstring>& arr;
  68. int low;
  69. int high;
  70. bool ascending;
  71. };
  72.  
  73. DWORD WINAPI QuickSortThread(LPVOID lpParam) {
  74. ThreadData* data = static_cast<ThreadData*>(lpParam);
  75. quickSortRange(data->arr, data->low, data->high, data->ascending);
  76.  
  77. delete data;
  78. return 0;
  79. }
  80.  
  81. void quickSortMultithread(std::vector<std::wstring>& arr, int numThreads, bool ascending) {
  82. if (numThreads <= 1 || arr.size() <= 1) {
  83. quickSortRange(arr, 0, arr.size() - 1, ascending);
  84. return;
  85. }
  86.  
  87. std::vector<HANDLE> threadHandles;
  88. for (int i = 0; i < numThreads; i++) {
  89. ThreadData* data = new ThreadData{ arr, 0, (int)arr.size() - 1, ascending };
  90. HANDLE hThread = CreateThread(NULL, 0, QuickSortThread, data, 0, NULL);
  91. threadHandles.push_back(hThread);
  92. }
  93.  
  94. WaitForMultipleObjects(numThreads, threadHandles.data(), TRUE, INFINITE);
  95.  
  96. for (HANDLE hThread : threadHandles) {
  97. CloseHandle(hThread);
  98. }
  99. }
  100.  
  101. int main() {
  102. setlocale(LC_CTYPE, "");
  103. srand(static_cast<unsigned int>(time(nullptr)));
  104.  
  105. const int LENGTH_OF_STRINGS = 10;
  106. const int AMOUNT_OF_OUTPUT_STRINGS_PER_PART = 20;
  107.  
  108. int amountOfStrings;
  109. int amountOfThreads;
  110. bool ascending;
  111.  
  112. std::wcout << "Enter the number of threads: ";
  113. std::wcin >> amountOfThreads;
  114. std::wcout << "Enter the number of strings: ";
  115. std::wcin >> amountOfStrings;
  116. std::wcout << "Sort in ascending (1) or descending (0) order: ";
  117. std::wcin >> ascending;
  118.  
  119. std::locale::global(std::locale("en_US.UTF-8"));
  120. std::vector<std::wstring> strings;
  121. auto start = std::chrono::high_resolution_clock::now();
  122. for (int i = 0; i < amountOfStrings; i++) {
  123. strings.push_back(generateRandomText(LENGTH_OF_STRINGS));
  124. }
  125. auto end = std::chrono::high_resolution_clock::now();
  126. auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  127.  
  128. std::wcout << amountOfStrings << " strings has been successfully generated in " << duration.count() / 1000.0 << " seconds" << std::endl;
  129.  
  130. start = std::chrono::high_resolution_clock::now();
  131. quickSortMultithread(strings, amountOfThreads, ascending);
  132. end = std::chrono::high_resolution_clock::now();
  133. duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  134.  
  135. std::wcout << "--------------------------------------------------------------\n";
  136. std::wcout << "First " << AMOUNT_OF_OUTPUT_STRINGS_PER_PART << " sorted strings : \n";
  137. std::wcout << "--------------------------------------------------------------\n";
  138. for (int i = 0; i < AMOUNT_OF_OUTPUT_STRINGS_PER_PART; i++) {
  139. std::wcout << strings[i] << std::endl;
  140. }
  141.  
  142. int upperPart = std::ceil(static_cast<double>(AMOUNT_OF_OUTPUT_STRINGS_PER_PART) / 2);
  143. int lowerPart = std::floor(static_cast<double>(AMOUNT_OF_OUTPUT_STRINGS_PER_PART) / 2);
  144.  
  145. std::wcout << "--------------------------------------------------------------\n";
  146. std::wcout << "Middle " << AMOUNT_OF_OUTPUT_STRINGS_PER_PART << " sorted strings:\n";
  147. std::wcout << "--------------------------------------------------------------\n";
  148. for (int i = amountOfStrings / 2 - lowerPart; i < amountOfStrings / 2 + upperPart; i++) {
  149. std::wcout << strings[i] << std::endl;
  150. }
  151.  
  152. std::wcout << "--------------------------------------------------------------\n";
  153. std::wcout << "Last " << AMOUNT_OF_OUTPUT_STRINGS_PER_PART << " sorted strings:\n";
  154. std::wcout << "--------------------------------------------------------------\n";
  155. for (int i = amountOfStrings - AMOUNT_OF_OUTPUT_STRINGS_PER_PART; i < amountOfStrings; i++) {
  156. std::wcout << strings[i] << std::endl;
  157. }
  158.  
  159. std::wcout << "Time taken for sorting: " << duration.count() / 1000.0 << " seconds" << std::endl;
  160.  
  161. exit(0);
  162. }
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement