Interstellar12312

Modified Probability Quicksort Algorithm

Dec 1st, 2025
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <random>
  5. #include <chrono>
  6.  
  7. // Modified Quicksort with three element rule
  8. // Contact Email [email protected]
  9. // Created by I.A
  10.  
  11. // Configuration Constants
  12. const int NUM_ELEMENTS = 1000000; // 1 million elements
  13. const int NUM_TRIALS = 1000; // 1000 trials
  14.  
  15. // Helper function to print vector (for debugging small sizes)
  16. void printVector(const std::vector<int>& arr) {
  17. for (int num : arr) {
  18. std::cout << num << " ";
  19. }
  20. std::cout << std::endl;
  21. }
  22.  
  23. /**
  24. * Applies the specific rule requested for subarrays of size 3.
  25. */
  26. void apply_three_element_rule(std::vector<int>& arr, int low, int high) {
  27. // Corrected condition: size 3 implies high - low == 2 (e.g., indices 0, 1, 2)
  28. if (high - low == 2) {
  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.  
  50. // Standard Hoare partition scheme
  51. int partition(std::vector<int>& arr, int low, int high) {
  52. int pivot = arr[low];
  53. int i = low - 1;
  54. int j = high + 1;
  55.  
  56. while (true) {
  57. do {
  58. i++;
  59. } while (arr[i] < pivot);
  60.  
  61. do {
  62. j--;
  63. } while (arr[j] > pivot);
  64.  
  65. if (i >= j)
  66. return j;
  67.  
  68. std::swap(arr[i], arr[j]);
  69. }
  70. }
  71.  
  72. /**
  73. * Quicksort implementation.
  74. * Integrates the apply_three_element_rule when the subarray size is 3.
  75. */
  76. void quickSort(std::vector<int>& arr, int low, int high) {
  77. if (low < high) {
  78. // Check for specific 3-element case
  79. if (high - low == 2) {
  80. apply_three_element_rule(arr, low, high);
  81. }
  82.  
  83. // Partition the array
  84. int p = partition(arr, low, high);
  85.  
  86. // Recursively sort elements before and after partition
  87. quickSort(arr, low, p);
  88. quickSort(arr, p + 1, high);
  89. }
  90. }
  91.  
  92. int main() {
  93. std::cout << "Starting Benchmark..." << std::endl;
  94. std::cout << "Elements per array: " << NUM_ELEMENTS << std::endl;
  95. std::cout << "Total Trials: " << NUM_TRIALS << std::endl;
  96.  
  97. // Setup random number generation
  98. std::random_device rd;
  99. std::mt19937 gen(rd());
  100. std::uniform_int_distribution<> distrib(1, 10000000);
  101.  
  102. // Pre-allocate vector
  103. std::vector<int> data(NUM_ELEMENTS);
  104.  
  105. // Variable to hold ONLY the time spent sorting
  106. std::chrono::duration<double> total_sort_duration(0);
  107.  
  108. for (int t = 1; t <= NUM_TRIALS; ++t) {
  109. // 1. Generate Random Numbers (NOT TIMED)
  110. for (int i = 0; i < NUM_ELEMENTS; ++i) {
  111. data[i] = distrib(gen);
  112. }
  113.  
  114. // 2. Start Timer (Start "Stopwatch")
  115. auto start = std::chrono::high_resolution_clock::now();
  116.  
  117. // 3. Run Quicksort
  118. quickSort(data, 0, NUM_ELEMENTS - 1);
  119.  
  120. // 4. Stop Timer (Stop "Stopwatch")
  121. auto end = std::chrono::high_resolution_clock::now();
  122.  
  123. // 5. Add difference to accumulator
  124. total_sort_duration += (end - start);
  125.  
  126. // Optional: Simple check to ensure sorted (checks only first trial)
  127. if (t == 1) {
  128. bool sorted = true;
  129. for (size_t i = 0; i < data.size() - 1; ++i) {
  130. if (data[i] > data[i+1]) {
  131. sorted = false;
  132. break;
  133. }
  134. }
  135. if (!sorted) {
  136. std::cerr << "Error: Array not sorted on trial 1!" << std::endl;
  137. return 1;
  138. }
  139. }
  140.  
  141. // Progress logging
  142. if (t % 10 == 0 || t == NUM_TRIALS) {
  143. std::cout << "Completed Trial " << t << "/" << NUM_TRIALS << "\r" << std::flush;
  144. }
  145. }
  146.  
  147. std::cout << "\n\nBenchmark Complete." << std::endl;
  148.  
  149. // Output stats based on the accumulated sort duration
  150. std::cout << "Total Sorting Time (excluding generation): " << total_sort_duration.count() << " seconds" << std::endl;
  151. std::cout << "Average Time per Sort: " << total_sort_duration.count() / NUM_TRIALS << " seconds" << std::endl;
  152.  
  153. return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment