Advertisement
Vladislav_Bezruk

quick and insertion sort compare

Oct 16th, 2021 (edited)
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <stdlib.h>
  4.  
  5. unsigned long long sortTime, aOperations, cOperations;
  6.  
  7. using namespace std;
  8.  
  9. bool isSorted(int arr[], int n) {
  10.     for (int i = 0; i < n - 1; i++) {
  11.         if (arr[i] > arr[i + 1]) return false;
  12.     }
  13.     return true;
  14. }
  15.  
  16. void insertionSort(int arr[], int n) {
  17.     for (int i = 1, j = i; i < n; i++, j = i) {
  18.         while (j > 0 && arr[j] < arr[j - 1]) {
  19.             swap(arr[j], arr[j - 1]);
  20.             j--;
  21.         }  
  22.     }
  23. }
  24.  
  25. void insertionSortwithOperations(int arr[], int n) {
  26.     for (int i = 1, j = i; i < n; i++, j = i) {
  27.         while (j > 0 && arr[j] < arr[j - 1]) {
  28.             cOperations++;
  29.             swap(arr[j], arr[j - 1]);
  30.             aOperations += 3;
  31.             j--;
  32.         }
  33.         cOperations++; 
  34.     }
  35. }
  36.  
  37. void quickSort(int arr[], int a, int b) {
  38.     int l = a, r = b;
  39.     int m = arr[(a + b) / 2];
  40.  
  41.     while (l <= r) {
  42.         while (arr[l] < m) l++;
  43.         while (arr[r] > m) r--;
  44.         if (l <= r) {
  45.             if (l < r)
  46.                 swap(arr[l], arr[r]);
  47.             ++l; --r;
  48.         }
  49.     }
  50.     if (a < r) quickSort(arr, a, r);
  51.     if (l < b) quickSort(arr, l, b);
  52. }
  53.  
  54. void quickSortwithOperations(int arr[], int a, int b) {
  55.     int l = a, r = b;
  56.     int m = arr[(a + b) / 2];
  57.  
  58.     while (l <= r) {
  59.         while (arr[l] < m) {
  60.             l++;
  61.             cOperations++; 
  62.         }
  63.         cOperations++;
  64.         while (arr[r] > m) {
  65.             r--;
  66.             cOperations++;
  67.         }
  68.         cOperations++;
  69.         if (l <= r) {
  70.             if (l < r) {
  71.                 aOperations += 3;
  72.                 swap(arr[l], arr[r]);
  73.             }
  74.             ++l; --r;
  75.         }
  76.     }
  77.     if (a < r) quickSort(arr, a, r);
  78.     if (l < b) quickSort(arr, l, b);
  79. }
  80.  
  81. void insertionSortBenchmark(int arr[], int n) {
  82.     int bArr[n];
  83.    
  84.     for (int i = 0; i < n; i++)
  85.         bArr[i] = arr[i];
  86.    
  87.     sortTime = clock();
  88.    
  89.     insertionSort(bArr, n);
  90.    
  91.     sortTime = clock() - sortTime; 
  92.    
  93.     for (int i = 0; i < n; i++)
  94.         bArr[i] = arr[i];
  95.        
  96.     aOperations = cOperations = 0; 
  97.    
  98.     insertionSortwithOperations(bArr, n);
  99.    
  100.     cout << "===Insertion=Sort=Benchmark===" << endl;
  101.     cout << "n = " << n << endl;
  102.     cout << "time = " << sortTime << " milliseconds" << endl;
  103.     cout << "assignment operations = " << aOperations << endl;
  104.     cout << "comparison operations = " << cOperations << endl;
  105.     cout << "is sorted = " << isSorted(bArr, n) << endl;
  106.     cout << "==============================" << endl << endl;
  107. }
  108.  
  109. void quickSortBenchmark(int arr[], int n) {
  110.     int bArr[n];
  111.    
  112.     for (int i = 0; i < n; i++)
  113.         bArr[i] = arr[i];
  114.    
  115.     sortTime = clock();
  116.    
  117.     quickSort(bArr, 0, n);
  118.    
  119.     sortTime = clock() - sortTime; 
  120.    
  121.     for (int i = 0; i < n; i++)
  122.         bArr[i] = arr[i];
  123.        
  124.     aOperations = cOperations = 0; 
  125.    
  126.     quickSortwithOperations(bArr, 0, n);
  127.    
  128.     cout << "=====Quick=Sort=Benchmark=====" << endl;
  129.     cout << "n = " << n << endl;
  130.     cout << "time = " << sortTime << " milliseconds" << endl;
  131.     cout << "assignment operations = " << aOperations << endl;
  132.     cout << "comparison operations = " << cOperations << endl;
  133.     cout << "is sorted = " << isSorted(bArr, n) << endl;
  134.     cout << "==============================" << endl << endl;
  135. }
  136.  
  137. int getRandom(int max) {
  138.     return rand() % max + 1;
  139. }
  140.  
  141. void generateRandom(int arr[], int n) {
  142.     for (int i = 0; i < n; i++) {
  143.         arr[i] = getRandom(n);
  144.     }
  145. }
  146.  
  147. void generateGrowth(int arr[], int n) {
  148.     for (int i = 0; i < n; i++) {
  149.         arr[i] = i;
  150.     }
  151. }
  152.  
  153. void generateDesc(int arr[], int n) {
  154.     for (int i = 0; i < n; i++) {
  155.         arr[i] = n - i - 1;
  156.     }
  157. }
  158.  
  159. int main() {
  160.     srand(time(NULL));
  161.    
  162.     const int tN[] = {10, 100, 1000, 10000};
  163.    
  164.     for (int n : tN) {
  165.         int arr[n];
  166.        
  167.         cout << "by random: " << endl;
  168.         generateRandom(arr, n);
  169.         quickSortBenchmark(arr, n);
  170.         insertionSortBenchmark(arr, n);
  171.        
  172.         cout << "by growth: " << endl;
  173.         generateGrowth(arr, n);
  174.         quickSortBenchmark(arr, n);
  175.         insertionSortBenchmark(arr, n);
  176.        
  177.         cout << "by descending: " << endl;
  178.         generateDesc(arr, n);
  179.         quickSortBenchmark(arr, n);
  180.         insertionSortBenchmark(arr, n);
  181.     }
  182.    
  183.     return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement