Interstellar12312

Modified Insertion Sort vs Standard Insertion Sort

Feb 25th, 2026 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <chrono>
  4. #include <random>
  5. #include <algorithm>
  6. #include <iomanip>
  7.  
  8. // Custom insertion sort vs standard insertion sort
  9. // Contact Email [email protected]
  10. // Created by I.A
  11.  
  12. void standardInsertionSort(std::vector<int>& arr) {
  13.     int n = arr.size();
  14.     for (int i = 1; i < n; i++) {
  15.         int key = arr[i];
  16.         int j = i - 1;
  17.         while (j >= 0 && arr[j] > key) {
  18.             arr[j + 1] = arr[j];
  19.             j--;
  20.         }
  21.         arr[j + 1] = key;
  22.     }
  23. }
  24.  
  25. void customJumpSort(std::vector<int>& arr) {
  26.     int n = arr.size();
  27.     for (int i = 1; i < n; i++) {
  28.         int key = arr[i];
  29.         int j = i - 1;
  30.         while (j >= 0 && arr[j] > key) {
  31.             if (j >= 1) {
  32.                 arr[j + 1] = arr[j];
  33.                 if (arr[j - 1] > key) {
  34.                     arr[j] = arr[j - 1];
  35.                     j -= 2;
  36.                 } else {
  37.                     arr[j] = key;
  38.                     goto placed;
  39.                 }
  40.             } else {
  41.                 arr[j + 1] = arr[j];
  42.                 j--;
  43.             }
  44.         }
  45.         arr[j + 1] = key;
  46.         placed:;
  47.     }
  48. }
  49.  
  50. int main() {
  51.     const int NUM_ELEMENTS = 32;
  52.     const int NUM_TRIALS = 1000000;
  53.  
  54.     std::random_device rd;
  55.     std::mt19937 gen(rd());
  56.     std::uniform_int_distribution<> dist(1, 100000);
  57.  
  58.     double totalStd = 0, totalJump = 0;
  59.  
  60.     for (int t = 0; t < NUM_TRIALS; ++t) {
  61.         std::vector<int> data1(NUM_ELEMENTS);
  62.         for (int& x : data1) x = dist(gen);
  63.         std::vector<int> data2 = data1;
  64.  
  65.         // Benchmark Standard
  66.         auto s1 = std::chrono::steady_clock::now();
  67.         standardInsertionSort(data1);
  68.         auto e1 = std::chrono::steady_clock::now();
  69.         totalStd += std::chrono::duration<double>(e1 - s1).count();
  70.  
  71.         // Benchmark Custom
  72.         auto s2 = std::chrono::steady_clock::now();
  73.         customJumpSort(data2);
  74.         auto e2 = std::chrono::steady_clock::now();
  75.         totalJump += std::chrono::duration<double>(e2 - s2).count();
  76.  
  77.         // Verify Logic
  78.         if (!std::is_sorted(data2.begin(), data2.end())) {
  79.             std::cout << "CRITICAL ERROR: Custom Sort Failed!" << std::endl;
  80.             return 1;
  81.         }
  82.     }
  83.  
  84.     // Changed output to Total Time
  85.     std::cout << std::left << std::setw(20) << "Algorithm" << " | " << "Total Time (s)" << std::endl;
  86.     std::cout << "--------------------------------------------" << std::endl;
  87.     std::cout << std::setw(20) << "Standard Insertion" << " | " << std::fixed << std::setprecision(6) << totalStd << std::endl;
  88.     std::cout << std::setw(20) << "Custom insertion Sort" << " | " << std::fixed << std::setprecision(6) << totalJump << std::endl;
  89.  
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment