Advertisement
fatalryuu

multithread utf8 string sort

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