Advertisement
fatalryuu

Untitled

Oct 17th, 2023
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 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.  
  10. std::string generateRandomText(int length) {
  11. const char characters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  12. std::string randomText;
  13.  
  14. for (int i = 0; i < length; i++) {
  15. int index = rand() % (sizeof(characters) - 1);
  16. randomText += characters[index];
  17. }
  18.  
  19. return randomText;
  20. }
  21.  
  22. // Quick sort function for a given range [low, high]
  23. void quickSortRange(std::vector<std::string>& arr, int low, int high, bool ascending) {
  24. if (low < high) {
  25. int pivotIndex = low + (high - low) / 2;
  26. std::string pivot = arr[pivotIndex];
  27. int i = low;
  28. int j = high;
  29.  
  30. while (i <= j) {
  31. if (ascending) {
  32. while (arr[i] < pivot) {
  33. i++;
  34. }
  35. while (arr[j] > pivot) {
  36. j--;
  37. }
  38. }
  39. else {
  40. while (arr[i] > pivot) {
  41. i++;
  42. }
  43. while (arr[j] < pivot) {
  44. j--;
  45. }
  46. }
  47.  
  48. if (i <= j) {
  49. std::swap(arr[i], arr[j]);
  50. i++;
  51. j--;
  52. }
  53. }
  54.  
  55. if (low < j) {
  56. quickSortRange(arr, low, j, ascending);
  57. }
  58. if (i < high) {
  59. quickSortRange(arr, i, high, ascending);
  60. }
  61. }
  62. }
  63.  
  64. struct ThreadData {
  65. std::vector<std::string>& arr;
  66. int low;
  67. int high;
  68. bool ascending;
  69. };
  70.  
  71. // Thread entry point function
  72. DWORD WINAPI QuickSortThread(LPVOID lpParam) {
  73. ThreadData* data = static_cast<ThreadData*>(lpParam);
  74. quickSortRange(data->arr, data->low, data->high, data->ascending);
  75.  
  76. delete data;
  77. return 0;
  78. }
  79.  
  80. // Multithreaded quick sort function
  81. void quickSortMultithread(std::vector<std::string>& 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. int numElements = arr.size();
  88. int elementsPerThread = numElements / numThreads;
  89.  
  90. std::vector<HANDLE> threadHandles;
  91. for (int i = 0; i < numThreads; i++) {
  92. int start = i * elementsPerThread;
  93. int end = (i == numThreads - 1) ? numElements - 1 : start + elementsPerThread - 1;
  94. ThreadData* data = new ThreadData{ arr, start, end, ascending };
  95. HANDLE hThread = CreateThread(NULL, 0, QuickSortThread, data, 0, NULL);
  96. threadHandles.push_back(hThread);
  97. }
  98.  
  99. WaitForMultipleObjects(numThreads, &threadHandles[0], TRUE, INFINITE);
  100.  
  101. for (HANDLE hThread : threadHandles) {
  102. CloseHandle(hThread);
  103. }
  104. }
  105.  
  106. int main() {
  107. srand(static_cast<unsigned int>(time(nullptr)));
  108.  
  109. const int LENGTH_OF_STRINGS = 10;
  110.  
  111. int amountOfStrings = 1000;
  112. int amountOfThreads = 4;
  113. bool ascending = true;
  114. bool isAgain = false;
  115.  
  116. do {
  117. std::cout << "Enter the number of threads: ";
  118. std::cin >> amountOfThreads;
  119. std::cout << "Enter the number of strings: ";
  120. std::cin >> amountOfStrings;
  121.  
  122. std::cout << "Sort in ascending (1) or descending (0) order: ";
  123. std::cin >> ascending;
  124.  
  125. std::vector<std::string> strings;
  126. for (int i = 0; i < amountOfStrings; i++) {
  127. strings.push_back(generateRandomText(LENGTH_OF_STRINGS));
  128. }
  129.  
  130. auto start = std::chrono::high_resolution_clock::now();
  131. quickSortMultithread(strings, amountOfThreads, ascending);
  132. auto end = std::chrono::high_resolution_clock::now();
  133. auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  134.  
  135. std::cout << "First 5 sorted strings:\n";
  136. for (int i = 0; i < 5; i++) {
  137. std::cout << strings[i] << std::endl;
  138. }
  139.  
  140. std::cout << "Middle 5 sorted strings:\n";
  141. for (int i = amountOfStrings / 2 - 2; i < amountOfStrings / 2 + 3; i++) {
  142. std::cout << strings[i] << std::endl;
  143. }
  144.  
  145. std::cout << "Last 5 sorted strings:\n";
  146. for (int i = amountOfStrings - 5; i < amountOfStrings; i++) {
  147. std::cout << strings[i] << std::endl;
  148. }
  149.  
  150. std::cout << "Total time taken for quickSortMultithread: " << duration.count() / 1000.0 << " seconds" << std::endl;
  151.  
  152. std::cout << "Do you want to use it again? (yes - 1, no - 0)" << std::endl;
  153. std::cin >> isAgain;
  154.  
  155. } while (isAgain);
  156.  
  157. return 0;
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement