Interstellar12312

Standard Quicksort Algorithm

Dec 1st, 2025
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <random>
  5. #include <chrono>
  6.  
  7. // Standard Quicksort
  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. // Standard Hoare partition scheme
  24. int partition(std::vector<int>& arr, int low, int high) {
  25. // Hoare's partition uses the first element as the pivot
  26. int pivot = arr[low];
  27. int i = low - 1;
  28. int j = high + 1;
  29.  
  30. while (true) {
  31. // Find element on the left that is >= pivot
  32. do {
  33. i++;
  34. } while (arr[i] < pivot);
  35.  
  36. // Find element on the right that is <= pivot
  37. do {
  38. j--;
  39. } while (arr[j] > pivot);
  40.  
  41. // If pointers cross, partition is complete
  42. if (i >= j)
  43. return j;
  44.  
  45. // Swap elements at i and j
  46. std::swap(arr[i], arr[j]);
  47. }
  48. }
  49.  
  50. /**
  51. * Standard Quicksort implementation (Recursive).
  52. */
  53. void quickSort(std::vector<int>& arr, int low, int high) {
  54. if (low < high) {
  55. // p is the partitioning index, arr[p] is now at the right place
  56. int p = partition(arr, low, high);
  57.  
  58. // Recursively sort elements before and after partition
  59. quickSort(arr, low, p);
  60. quickSort(arr, p + 1, high);
  61. }
  62. }
  63.  
  64. int main() {
  65. std::cout << "Starting Benchmark (Standard Quicksort)..." << std::endl;
  66. std::cout << "Elements per array: " << NUM_ELEMENTS << std::endl;
  67. std::cout << "Total Trials: " << NUM_TRIALS << std::endl;
  68.  
  69. // Setup random number generation
  70. std::random_device rd;
  71. std::mt19937 gen(rd());
  72. std::uniform_int_distribution<> distrib(1, 10000000);
  73.  
  74. // Pre-allocate vector
  75. std::vector<int> data(NUM_ELEMENTS);
  76.  
  77. // Variable to hold ONLY the time spent sorting
  78. std::chrono::duration<double> total_sort_duration(0);
  79.  
  80. for (int t = 1; t <= NUM_TRIALS; ++t) {
  81. // 1. Generate Random Numbers (Time NOT recorded here)
  82. for (int i = 0; i < NUM_ELEMENTS; ++i) {
  83. data[i] = distrib(gen);
  84. }
  85.  
  86. // 2. Start Timer (Start "Stopwatch")
  87. auto start = std::chrono::high_resolution_clock::now();
  88.  
  89. // 3. Run Quicksort (THIS TIME IS RECORDED)
  90. quickSort(data, 0, NUM_ELEMENTS - 1);
  91.  
  92. // 4. Stop Timer (Stop "Stopwatch")
  93. auto end = std::chrono::high_resolution_clock::now();
  94.  
  95. // 5. Add difference to accumulator
  96. total_sort_duration += (end - start);
  97.  
  98.  
  99. // Optional: Simple check to ensure sorted (checks only first trial to save time)
  100. if (t == 1) {
  101. bool sorted = true;
  102. for (size_t i = 0; i < data.size() - 1; ++i) {
  103. if (data[i] > data[i+1]) {
  104. sorted = false;
  105. break;
  106. }
  107. }
  108. if (!sorted) {
  109. std::cerr << "Error: Array not sorted on trial 1!" << std::endl;
  110. return 1;
  111. }
  112. }
  113.  
  114. // Progress logging
  115. if (t % 10 == 0 || t == NUM_TRIALS) {
  116. std::cout << "Completed Trial " << t << "/" << NUM_TRIALS << "\r" << std::flush;
  117. }
  118. }
  119.  
  120. std::cout << "\n\nBenchmark Complete." << std::endl;
  121.  
  122. // Output stats based on the accumulated sort duration
  123. std::cout << "Total Sorting Time (excluding generation): " << total_sort_duration.count() << " seconds" << std::endl;
  124. std::cout << "Average Time per Sort: " << total_sort_duration.count() / NUM_TRIALS << " seconds" << std::endl;
  125.  
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment