Guest User

Untitled

a guest
Dec 2nd, 2025
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.88 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <random>
  5. #include <chrono>
  6. #include <iomanip>
  7.  
  8. // Modified Quicksort with three element rule
  9. // Contact Email [email protected]
  10. // Created by I.A
  11.  
  12. // Configuration Constants
  13. const int NUM_ELEMENTS = 1000000;
  14. const int NUM_TRIALS = 10;
  15. const int NUM_REPEATS = 20;
  16.  
  17. // Helper function to print vector (for debugging small sizes)
  18. void printVector(const std::vector<int>& arr) {
  19.     for (int num : arr) {
  20.         std::cout << num << " ";
  21.     }
  22.     std::cout << std::endl;
  23. }
  24.  
  25. /**
  26.  * Applies the specific rule requested for subarrays of size 3.
  27.  */
  28. void apply_three_element_rule(std::vector<int>& arr, int low, int high) {
  29.     int pivot = arr[low];      // First number as pivot
  30.     int third_num = arr[high]; // Third number to compare
  31.  
  32.     if (third_num <= pivot) {
  33.         // Swap all 3 to make pivot middle, third_num first
  34.         int temp_mid = arr[low + 1];
  35.  
  36.         arr[high] = temp_mid;
  37.         arr[low + 1] = pivot;
  38.         arr[low] = third_num;
  39.     } else {
  40.         // Swap all 3 to make pivot middle, third_num last
  41.         int temp_mid = arr[low + 1];
  42.  
  43.         arr[low] = temp_mid;
  44.         arr[low + 1] = pivot;
  45.         arr[high] = third_num;
  46.     }
  47. }
  48.  
  49. // Standard Hoare partition scheme
  50. int partition(std::vector<int>& arr, int low, int high) {
  51.     int pivot = arr[(low + high) / 2];
  52.     int i = low - 1;
  53.     int j = high + 1;
  54.  
  55.     while (true) {
  56.         do {
  57.             i++;
  58.         } while (arr[i] < pivot);
  59.  
  60.         do {
  61.             j--;
  62.         } while (arr[j] > pivot);
  63.  
  64.         if (i >= j)
  65.             return j;
  66.  
  67.         std::swap(arr[i], arr[j]);
  68.     }
  69. }
  70.  
  71. /**
  72.  * Quicksort implementation.
  73.  * Integrates the apply_three_element_rule when the subarray size is 3.
  74.  */
  75. void quickSortProb(std::vector<int>& arr, int low, int high) {
  76.     if (low < high) {
  77.         // Check for specific 3-element case
  78.         if (high - low == 2) {
  79.             apply_three_element_rule(arr, low, high);
  80.         }
  81.  
  82.         // Partition the array
  83.         int p = partition(arr, low, high);
  84.  
  85.         // Recursively sort elements before and after partition
  86.         quickSortProb(arr, low, p);
  87.         quickSortProb(arr, p + 1, high);
  88.     }
  89. }
  90.  
  91. /**
  92.  * Standard Quicksort implementation (Recursive).
  93.  */
  94. void quickSort(std::vector<int>& arr, int low, int high) {
  95.     if (low < high) {
  96.         // p is the partitioning index, arr[p] is now at the right place
  97.         int p = partition(arr, low, high);
  98.  
  99.         // Recursively sort elements before and after partition
  100.         quickSort(arr, low, p);
  101.         quickSort(arr, p + 1, high);
  102.     }
  103. }
  104.  
  105. void QuickSortStd(std::vector<int>& arr, int low, int high) {
  106.     std::sort(&arr[low], &arr[high] + 1);
  107. }
  108.  
  109. template<typename Function> int runBenchmark(Function testedFunction, const std::vector<int>& data, bool checkIfCorrectOutput) {
  110.     std::vector<int> executionTimes;
  111.     for (int i = 1; i <= NUM_REPEATS; ++i) {
  112.         auto copiedData = data;
  113.         auto start = std::chrono::steady_clock::now();
  114.         testedFunction(copiedData, 0, NUM_ELEMENTS - 1);
  115.         auto end = std::chrono::steady_clock::now();
  116.         executionTimes.push_back(std::chrono::duration_cast<std::chrono::microseconds>(end - start).count());
  117.  
  118.         if (checkIfCorrectOutput) {
  119.             bool sorted = true;
  120.             for (size_t i = 0; i < copiedData.size() - 1; ++i) {
  121.                 if (copiedData[i] > copiedData[i + 1]) {
  122.                     sorted = false;
  123.                     break;
  124.                 }
  125.             }
  126.             if (!sorted) {
  127.                 std::cerr << "Error: Array not sorted on trial 1!" << std::endl;
  128.             }
  129.         }
  130.     }
  131.     return *std::min_element(executionTimes.begin(), executionTimes.end());
  132. }
  133.  
  134. int main() {
  135.     std::cout << "Starting Benchmark..." << std::endl;
  136.     std::cout << "Elements per array: " << NUM_ELEMENTS << std::endl;
  137.     std::cout << "Total Trials: " << NUM_TRIALS << std::endl;
  138.  
  139.     // Setup random number generation
  140.     std::mt19937 gen(0x12345678);
  141.     std::uniform_int_distribution<> distrib(1, 10000000);
  142.  
  143.     // Pre-allocate vector
  144.     std::vector<int> data(NUM_ELEMENTS);
  145.  
  146.     std::cout << std::left;
  147.     std::cout << std::setw(10) << "Trial nr" << " | "
  148.         << std::setw(20) << "Quicksort [us]" << " | "
  149.         << std::setw(20) << "QuicksortProb [us]" << " | "
  150.         << std::setw(20) << "QuicksortStd [us]" << "\n";
  151.    
  152.     int sumOfMinTimesQuicksort = 0;
  153.     int sumOfMinTimesQuicksortProb = 0;
  154.     int sumOfMinTimesQuicksortStd = 0;
  155.  
  156.     for (int t = 1; t <= NUM_TRIALS; ++t) {
  157.         for (int i = 0; i < NUM_ELEMENTS; ++i) {
  158.             data[i] = distrib(gen);
  159.         }
  160.         int minTimeQuicksort = runBenchmark(quickSort, data, true);
  161.         int minTimeQuicksortProb = runBenchmark(quickSortProb, data, true);
  162.         int minTimeQuicksortStd = runBenchmark(QuickSortStd, data, true);
  163.         std::cout << std::setw(10) << t << " | "
  164.             << std::setw(20) << minTimeQuicksort << " | "
  165.             << std::setw(20) << minTimeQuicksortProb << " | "
  166.             << std::setw(20) << minTimeQuicksortStd << "\n";
  167.         sumOfMinTimesQuicksort += minTimeQuicksort;
  168.         sumOfMinTimesQuicksortProb += minTimeQuicksortProb;
  169.         sumOfMinTimesQuicksortStd += minTimeQuicksortStd;
  170.     }
  171.  
  172.     std::cout << std::setw(10) << "Total" << " | "
  173.         << std::setw(20) << sumOfMinTimesQuicksort << " | "
  174.         << std::setw(20) << sumOfMinTimesQuicksortProb << " | "
  175.         << std::setw(20) << sumOfMinTimesQuicksortStd << '\n';
  176.  
  177.     std::cout << std::setw(30) << "quicksortProb / quicksort " << " = " << double(sumOfMinTimesQuicksortProb) / sumOfMinTimesQuicksort << '\n';
  178.     std::cout << std::setw(30) << "quicksortProb / quicksortStd " << " = " << double(sumOfMinTimesQuicksortProb) / sumOfMinTimesQuicksortStd << '\n';
  179.  
  180.     return 0;
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment