Interstellar12312

Quicksort + Modified Insertion sort verses Quicksort + Standard Insertion Sort

Jul 21st, 2026
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <chrono>
  4. #include <random>
  5. #include <algorithm>
  6. #include <iomanip>
  7.  
  8. // Quicksort + Modified Insertion sort verses Quicksort + Standard Insertion Sort
  9. // Switches to Insertion sort <= 32 elements
  10. // Contact Email [email protected]
  11. // Created by I.A
  12.  
  13. // Cache Clearing
  14. // 32MB
  15. const size_t CACHE_SIZE_BYTES = 32 * 1024 * 1024;
  16. const size_t CACHE_SIZE_INTS = CACHE_SIZE_BYTES / sizeof(int);
  17. std::vector<int> cache_trasher(CACHE_SIZE_INTS, 0);
  18.  
  19. void flushCache() {
  20.     // Use volatile to prevent the compiler from optimizing this loop away
  21.     volatile int* p = cache_trasher.data();
  22.  
  23.     // Step by 16 integers (64 bytes) to touch every standard CPU cache line
  24.     for (size_t i = 0; i < CACHE_SIZE_INTS; i += 16) {
  25.         p[i] += 1;
  26.     }
  27. }
  28.  
  29. void standardInsertionSortSlice(std::vector<int>& arr, int low, int high) {
  30.     for (int i = low + 1; i <= high; i++) {
  31.         int key = arr[i];
  32.         int j = i - 1;
  33.         while (j >= low && arr[j] > key) {
  34.             arr[j + 1] = arr[j];
  35.             j--;
  36.         }
  37.         arr[j + 1] = key;
  38.     }
  39. }
  40.  
  41. void customJumpSortSlice(std::vector<int>& arr, int low, int high) {
  42.     for (int i = low + 1; i <= high; i++) {
  43.         int key = arr[i];
  44.         int j = i - 1;
  45.         while (j >= low && arr[j] > key) {
  46.             if (j >= low + 1) {
  47.                 arr[j + 1] = arr[j];
  48.                 if (arr[j - 1] > key) {
  49.                     arr[j] = arr[j - 1];
  50.                     j -= 2;
  51.                 } else {
  52.                     arr[j] = key;
  53.                     goto placed;
  54.                 }
  55.             } else {
  56.                 arr[j + 1] = arr[j];
  57.                 j--;
  58.             }
  59.         }
  60.         arr[j + 1] = key;
  61.         placed:;
  62.     }
  63. }
  64.  
  65. int medianOfThree(std::vector<int>& arr, int low, int high) {
  66.     int mid = low + (high - low) / 2;
  67.     if (arr[mid] < arr[low])
  68.         std::swap(arr[low], arr[mid]);
  69.     if (arr[high] < arr[low])
  70.         std::swap(arr[low], arr[high]);
  71.     if (arr[mid] < arr[high])
  72.         std::swap(arr[mid], arr[high]);
  73.     return arr[high];
  74. }
  75.  
  76. int partition(std::vector<int>& arr, int low, int high) {
  77.     int pivot = medianOfThree(arr, low, high);
  78.     int i = low - 1;
  79.  
  80.     for (int j = low; j < high; j++) {
  81.         if (arr[j] <= pivot) {
  82.             i++;
  83.             std::swap(arr[i], arr[j]);
  84.         }
  85.     }
  86.     std::swap(arr[i + 1], arr[high]);
  87.     return i + 1;
  88. }
  89.  
  90. // ---------------------------------------------------------
  91. // Hybrid QuickSort Algorithms (Cutoff threshold: 32 elements)
  92. // ---------------------------------------------------------
  93.  
  94. void quickSortStandardInsertion(std::vector<int>& arr, int low, int high) {
  95.     if (low < high) {
  96.         // Switch to insertion sort for subarrays with 32 or fewer elements
  97.         if (high - low + 1 <= 32) {
  98.             standardInsertionSortSlice(arr, low, high);
  99.             return;
  100.         }
  101.  
  102.         int pi = partition(arr, low, high);
  103.  
  104.         quickSortStandardInsertion(arr, low, pi - 1);
  105.         quickSortStandardInsertion(arr, pi + 1, high);
  106.     }
  107. }
  108.  
  109. void quickSortCustomJump(std::vector<int>& arr, int low, int high) {
  110.     if (low < high) {
  111.         // Switch to custom jump sort for subarrays with 32 or fewer elements
  112.         if (high - low + 1 <= 32) {
  113.             customJumpSortSlice(arr, low, high);
  114.             return;
  115.         }
  116.  
  117.         int pi = partition(arr, low, high);
  118.  
  119.         quickSortCustomJump(arr, low, pi - 1);
  120.         quickSortCustomJump(arr, pi + 1, high);
  121.     }
  122. }
  123.  
  124. // ---------------------------------------------------------
  125. // Main Benchmark
  126. // ---------------------------------------------------------
  127.  
  128. int main() {
  129.     const int NUM_ELEMENTS = 10000; // Increased array size to showcase QuickSort behavior
  130.     const int NUM_TRIALS = 10000;      // Adjusted trial count for balanced execution time
  131.  
  132.     std::random_device rd;
  133.     std::mt19937 gen(rd());
  134.     std::uniform_int_distribution<> dist(1, 100000);
  135.  
  136.     double totalQuickStd = 0, totalQuickJump = 0;
  137.  
  138.     for (int t = 0; t < NUM_TRIALS; ++t) {
  139.         std::vector<int> data1(NUM_ELEMENTS);
  140.         for (int& x : data1) x = dist(gen);
  141.         std::vector<int> data2 = data1;
  142.  
  143.         // Benchmark QuickSort with Standard Insertion
  144.         //flushCache();
  145.         auto s1 = std::chrono::steady_clock::now();
  146.         quickSortStandardInsertion(data1, 0, NUM_ELEMENTS - 1);
  147.         auto e1 = std::chrono::steady_clock::now();
  148.         totalQuickStd += std::chrono::duration<double>(e1 - s1).count();
  149.  
  150.         // Benchmark QuickSort with Custom Jump Sort
  151.         //flushCache();
  152.         auto s2 = std::chrono::steady_clock::now();
  153.         quickSortCustomJump(data2, 0, NUM_ELEMENTS - 1);
  154.         auto e2 = std::chrono::steady_clock::now();
  155.         totalQuickJump += std::chrono::duration<double>(e2 - s2).count();
  156.  
  157.         // Verify Logic
  158.         if (!std::is_sorted(data1.begin(), data1.end()) || !std::is_sorted(data2.begin(), data2.end())) {
  159.             std::cout << "CRITICAL ERROR: Array sorting failed!" << std::endl;
  160.             return 1;
  161.         }
  162.     }
  163.  
  164.     std::cout << std::left << std::setw(30) << "Algorithm" << " | " << "Total Time (s)" << std::endl;
  165.     std::cout << "--------------------------------------------------------" << std::endl;
  166.     std::cout << std::setw(30) << "QuickSort + Standard Insertion" << " | " << std::fixed << std::setprecision(6) << totalQuickStd << std::endl;
  167.     std::cout << std::setw(30) << "QuickSort + Custom Jump" << " | " << std::fixed << std::setprecision(6) << totalQuickJump << std::endl;
  168.  
  169.     return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment