Interstellar12312

Standard Insertion Sort

Dec 9th, 2025
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <random>
  5. #include <chrono>
  6.  
  7. // Benchmark Code (Baseline: Insertion Sort Only)
  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. * Standard Insertion Sort implementation.
  25. */
  26. void insertionSort(std::vector<int>& arr, int low, int high) {
  27. // Traverse through all elements from the second element (low + 1) to high
  28. for (int i = low + 1; i <= high; ++i) {
  29. int key = arr[i];
  30. int j = i - 1;
  31.  
  32. /* Move elements that are greater than key to one position ahead */
  33. while (j >= low && arr[j] > key) {
  34. arr[j + 1] = arr[j];
  35. j = j - 1;
  36. }
  37. // Insert the key into its correct position
  38. arr[j + 1] = key;
  39. }
  40. }
  41.  
  42. int main() {
  43. std::cout << "Starting Benchmark (Baseline: Insertion Sort Only)..." << std::endl;
  44. std::cout << "Elements per array: " << NUM_ELEMENTS << std::endl;
  45. std::cout << "Total Trials: " << NUM_TRIALS << std::endl;
  46.  
  47. // Setup random number generation
  48. std::random_device rd;
  49. std::mt19937 gen(rd());
  50. std::uniform_int_distribution<> distrib(1, 10000000);
  51.  
  52. // Pre-allocate vector
  53. std::vector<int> data(NUM_ELEMENTS);
  54. std::chrono::duration<double> total_sort_duration(0);
  55.  
  56. for (int t = 1; t <= NUM_TRIALS; ++t) {
  57. // 1. Generate Random Numbers (NOT TIMED)
  58. for (int i = 0; i < NUM_ELEMENTS; ++i) {
  59. data[i] = distrib(gen);
  60. }
  61.  
  62. // 2. Start Timer (Start "Stopwatch")
  63. auto start = std::chrono::steady_clock::now();
  64.  
  65. // 3. Run Insertion Sort (The only timed operation)
  66. insertionSort(data, 0, NUM_ELEMENTS - 1);
  67.  
  68. // 4. Stop Timer (Stop "Stopwatch")
  69. auto end = std::chrono::steady_clock::now();
  70.  
  71. // 5. Add difference to accumulator
  72. total_sort_duration += (end - start);
  73.  
  74. // Progress logging and verification (for brevity, removed redundant check)
  75. if (t % 100000 == 0 || t == NUM_TRIALS) {
  76. std::cout << "Completed Trial " << t << "/" << NUM_TRIALS << "\r" << std::flush;
  77. }
  78. }
  79.  
  80. std::cout << "\n\nBenchmark Complete." << std::endl;
  81. std::cout << "Total Sorting Time (Insertion Sort Only): " << total_sort_duration.count() << " seconds" << std::endl;
  82. std::cout << "Average Time per Sort: " << total_sort_duration.count() / NUM_TRIALS << " seconds" << std::endl;
  83.  
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment