Interstellar12312

(old) Modified Insertion Sort

Dec 9th, 2025 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <random>
  5. #include <chrono>
  6.  
  7. // Benchmark Code (Modified: Rule + Insertion Sort)
  8. // Contact Email [email protected]
  9. // Created by I.A
  10.  
  11. // Configuration Constants
  12. const int NUM_ELEMENTS = 32;
  13. const int NUM_TRIALS = 1000000;
  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. // The check for size 3 (high - low == 2) is assumed to pass
  28. // because the calling loop ensures it.
  29.  
  30. int pivot = arr[low];
  31. int third_num = arr[high];
  32.  
  33. if (third_num <= pivot) {
  34. // Swap all 3 to make pivot middle, third_num first
  35. int temp_mid = arr[low + 1];
  36.  
  37. arr[high] = temp_mid;
  38. arr[low + 1] = pivot;
  39. arr[low] = third_num;
  40. } else {
  41. // Swap all 3 to make pivot middle, third_num last
  42. int temp_mid = arr[low + 1];
  43.  
  44. arr[low] = temp_mid;
  45. arr[low + 1] = pivot;
  46. arr[high] = third_num;
  47. }
  48. }
  49.  
  50. /**
  51. * Standard Insertion Sort implementation.
  52. */
  53. void insertionSort(std::vector<int>& arr, int low, int high) {
  54. for (int i = low + 1; i <= high; ++i) {
  55. int key = arr[i];
  56. int j = i - 1;
  57.  
  58. while (j >= low && arr[j] > key) {
  59. arr[j + 1] = arr[j];
  60. j = j - 1;
  61. }
  62. arr[j + 1] = key;
  63. }
  64. }
  65.  
  66. int main() {
  67. std::cout << "Starting Benchmark (Exhaustive 3-Element Rule + Insertion Sort)..." << std::endl;
  68. std::cout << "Elements per array: " << NUM_ELEMENTS << std::endl;
  69. std::cout << "Total Trials: " << NUM_TRIALS << std::endl;
  70. std::cout << "NOTE: The three-element rule is now called on *every* group of 3 elements." << std::endl;
  71.  
  72. // Setup random number generation
  73. std::random_device rd;
  74. std::mt19937 gen(rd());
  75. std::uniform_int_distribution<> distrib(1, 10000000);
  76.  
  77. // Pre-allocate vector
  78. std::vector<int> data(NUM_ELEMENTS);
  79. std::chrono::duration<double> total_sort_duration(0);
  80.  
  81. for (int t = 1; t <= NUM_TRIALS; ++t) {
  82. // 1. Generate Random Numbers (NOT TIMED)
  83. for (int i = 0; i < NUM_ELEMENTS; ++i) {
  84. data[i] = distrib(gen);
  85. }
  86.  
  87. // 2. Start Timer (Start "Stopwatch")
  88. auto start = std::chrono::steady_clock::now();
  89.  
  90. // 3a. APPLY THE THREE-ELEMENT RULE TO ALL GROUPS OF THREE (TIMED)
  91. if (NUM_ELEMENTS >= 3) {
  92. for (int i = 0; i <= NUM_ELEMENTS - 3; i += 3) {
  93. apply_three_element_rule(data, i, i + 2);
  94. }
  95. }
  96.  
  97. // 3b. Run Insertion Sort (TIMED)
  98. insertionSort(data, 0, NUM_ELEMENTS - 1);
  99.  
  100. // 4. Stop Timer (Stop "Stopwatch")
  101. auto end = std::chrono::steady_clock::now();
  102.  
  103. // 5. Add difference to accumulator
  104. total_sort_duration += (end - start);
  105.  
  106. // Progress logging
  107. if (t % 100000 == 0 || t == NUM_TRIALS) {
  108. std::cout << "Completed Trial " << t << "/" << NUM_TRIALS << "\r" << std::flush;
  109. }
  110. }
  111.  
  112. std::cout << "\n\nBenchmark Complete." << std::endl;
  113. std::cout << "Total Sorting Time (Rule Loop + Insertion Sort): " << total_sort_duration.count() << " seconds" << std::endl;
  114. std::cout << "Average Time per Sort: " << total_sort_duration.count() / NUM_TRIALS << " seconds" << std::endl;
  115.  
  116. return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment