Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <ctime>
  4. #include <random>
  5. #include <vector>
  6. #include <chrono>
  7.  
  8. void bubbleSort(std::vector<int> &);
  9.  
  10. void insertionSort1(std::vector<int> &);
  11.  
  12. void insertionSort2(std::vector<int> &);
  13.  
  14. void selectionSort(std::vector<int> &);
  15.  
  16. bool checkIfSorted(std::vector<int> &);
  17.  
  18. void bubbleSort2(std::vector<int> &);
  19.  
  20. void bubbleSort3(std::vector<int> &);
  21.  
  22. void shellSort1(std::vector<int> &);
  23.  
  24. void shellSort2(std::vector<int> &);
  25.  
  26. void quickSort1(std::vector<int> &tab, int l, int r);
  27.  
  28. void quickSortRandom(std::vector<int> &, int, int);
  29.  
  30. void quickSortMedian(std::vector<int> &, int, int);
  31.  
  32. void quickSortAndInsertion(std::vector<int> &, int, int);
  33.  
  34. static std::random_device randomDevice;
  35. static std::mt19937 generator(randomDevice());
  36.  
  37.  
  38. int main() {
  39.     std::random_device rd;
  40.     std::mt19937 gen(rd());
  41.     std::uniform_int_distribution<> dis(0, 1000000);
  42.     const int tabSize = 128000;
  43.     std::vector<int> tab;
  44.     for (int i = 0; i < tabSize; ++i) {
  45.         tab.push_back(dis(gen));
  46.     }
  47.     std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
  48. //    bubbleSort(tab);
  49. //    selectionSort(tab);
  50. //    insertionSort1(tab);
  51. //    insertionSort2(tab);
  52. //    bubbleSort2(tab);
  53. //    bubbleSort3(tab);
  54. //    quickSort1(tab, 0, (int) tab.size());
  55. //    quickSortRandom(tab, 0, (int) tab.size());
  56. //    quickSortMedian(tab, 0, (int) tab.size());
  57.     quickSortAndInsertion(tab, 0, (int)tab.size());
  58. //    shellSort1(tab);
  59. //    shellSort2(tab);
  60.  
  61.     std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now();
  62.  
  63.     auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
  64.  
  65.     std::cout << std::endl
  66.               << "Duration of the sorting algorithm: "
  67.               << duration << std::endl;
  68.  
  69.     if (checkIfSorted(tab)) printf("Array is sorted correctly.");
  70.     return 0;
  71. }
  72.  
  73.  
  74. void shellSort1(std::vector<int> &tab) {
  75.     int h = 1;
  76.     while (h < tab.size() / 9) {
  77.         h = 3 * h + 1;
  78.     }
  79.     while (h > 0) {
  80.         for (int i = h; i < tab.size(); i++) {
  81.             int x = tab[i];
  82.             int j = i;
  83.             while (j >= h && x < tab[j - h]) {
  84.                 tab[j] = tab[j - h];
  85.                 j = j - h;
  86.             }
  87.             tab[j] = x;
  88.         }
  89.         h = h / 3;
  90.     }
  91. }
  92.  
  93. void shellSort2(std::vector<int> &tab) {
  94.     int h = static_cast<int>(tab.size() / 2);
  95.     while (h > 0) {
  96.         for (int i = h; i < tab.size(); i++) {
  97.             int x = tab[i];
  98.             int j = i;
  99.             while (j >= h && x < tab[j - h]) {
  100.                 tab[j] = tab[j - h];
  101.                 j = j - h;
  102.             }
  103.             tab[j] = x;
  104.         }
  105.         h = h / 2;
  106.     }
  107. }
  108.  
  109. void quickSort1(std::vector<int> &tab, int l, int r) {
  110.     if (l < r) {
  111.         int pivot = tab[l];
  112.         int s = l;
  113.         for (int i = l; i <= r; i++) {
  114.             if (tab[i] < pivot) {
  115.                 s++;
  116.                 std::swap(tab[s], tab[i]);
  117.             }
  118.         }
  119.         std::swap(tab[l], tab[s]);
  120.         quickSort1(tab, l, s - 1);
  121.         quickSort1(tab, s + 1, r);
  122.     }
  123. }
  124.  
  125. void quickSortRandom(std::vector<int> &tab, int l, int r) {
  126.     if (l < r) {
  127.         std::uniform_int_distribution<> dis(l, r);
  128.         int pivot_elem = dis(generator);
  129.         // Set pivot to first element:
  130.         std::swap(tab[l], tab[pivot_elem]);
  131.  
  132.  
  133.         int pivot = tab[l];
  134.         int s = l;
  135.         for (int i = l; i <= r; i++) {
  136.             if (tab[i] < pivot) {
  137.                 s++;
  138.                 std::swap(tab[s], tab[i]);
  139.             }
  140.         }
  141.         std::swap(tab[l], tab[s]);
  142.         quickSortRandom(tab, l, s - 1);
  143.         quickSortRandom(tab, s + 1, r);
  144.     }
  145. }
  146.  
  147. void quickSortMedian(std::vector<int> &tab, int l, int r) {
  148.     if (l < r) {
  149.         int j = r - 1;
  150.         int k = j / 2;
  151.         if (tab[j] < tab[k]) std::swap(tab[j], tab[k]);
  152.  
  153.         if (tab[j] < tab[l]) std::swap(tab[j], tab[l]);
  154.  
  155.         if (tab[k] < tab[l]) std::swap(tab[k], tab[l]);
  156.  
  157.         // Set pivot to the first element:
  158.         std::swap(tab[l], tab[k]);
  159.  
  160.  
  161.         int pivot = tab[l];
  162.         int s = l;
  163.         for (int i = l; i <= r; i++) {
  164.             if (tab[i] < pivot) {
  165.                 s++;
  166.                 std::swap(tab[s], tab[i]);
  167.             }
  168.         }
  169.         std::swap(tab[l], tab[s]);
  170.         quickSortMedian(tab, l, s - 1);
  171.         quickSortMedian(tab, s + 1, r);
  172.     }
  173. }
  174.  
  175. void quickSortInsert(std::vector<int> &arr, int l, int r) {
  176.     int temp, j;
  177.     for (int i = l; i < r; i++) {
  178.         temp = arr[i];
  179.         for (j = i - 1; j >= 0; j--) {
  180.             if (arr[j] > temp)
  181.                 arr[j + 1] = arr[j];
  182.             else
  183.                 break;
  184.         }
  185.         arr[j + 1] = temp;
  186.     }
  187. }
  188.  
  189. void quickSortAndInsertion(std::vector<int> &tab, int l, int r) {
  190.     if (l < r) {
  191.         if ((r - l) < 10) {
  192.             quickSortInsert(tab, l, r + 1);
  193.         } else {
  194.             int t = tab[l];
  195.             int s = l;
  196.             for (int i = l; i <= r; i++) {
  197.                 if (tab[i] < t) {
  198.                     s++;
  199.                     std::swap(tab[s], tab[i]);
  200.                 }
  201.             }
  202.             std::swap(tab[l], tab[s]);
  203.             quickSortAndInsertion(tab, l, s - 1);
  204.             quickSortAndInsertion(tab, s + 1, r);
  205.         }
  206.     }
  207. }
  208.  
  209.  
  210. void bubbleSort3(std::vector<int> &table) {
  211.     bool change = false;
  212.     int lastChangedPosition = 0;
  213.     do {
  214.         for (int i = lastChangedPosition; i < table.size() - 1; i++) {
  215.             if (table[i] > table[i + 1]) {
  216.                 std::swap(table[i], table[i + 1]);
  217.                 change = true;
  218.                 lastChangedPosition = i;
  219.             }
  220.         }
  221.         if (!change)
  222.             break;
  223.         change = false;
  224.         for (int i = lastChangedPosition - 1; i >= 0; i--) {
  225.             if (table[i] > table[i + 1]) {
  226.                 std::swap(table[i], table[i + 1]);
  227.                 change = true;
  228.                 lastChangedPosition = i;
  229.             }
  230.         }
  231.     } while (change);
  232. }
  233.  
  234. void bubbleSort2(std::vector<int> &table) {
  235.     bool change;
  236.     for (int i = 0; i < table.size(); ++i) {
  237.         change = false;
  238.         for (int j = 1; j < table.size(); ++j) {
  239.             if (table[j - 1] > table[j]) {
  240.                 std::swap(table[j - 1], table[j]);
  241.                 change = true;
  242.             }
  243.         }
  244.         if (!change)
  245.             break;
  246.     }
  247. }
  248.  
  249.  
  250. void insertionSort2(std::vector<int> &table) {
  251.     int valueMin = table[0];
  252.     int minIdx = 0;
  253.     int j;
  254.     for (int i = 1; i < table.size(); ++i) {
  255.         if (valueMin > table[i]) {
  256.             minIdx = i;
  257.             valueMin = table[i];
  258.         }
  259.     }
  260.     std::swap(table[0], table[minIdx]);
  261.     for (int i = 1; i < table.size(); ++i) {
  262.         valueMin = table[i];
  263.         j = i - 1;
  264.         while (valueMin < table[j]) {
  265.             table[j + 1] = table[j];
  266.             j = j - 1;
  267.         }
  268.         table[j + 1] = valueMin;
  269.     }
  270. }
  271.  
  272. void insertionSort1(std::vector<int> &table) {
  273.     int valueMin;
  274.     int j;
  275.     for (int i = 1; i < table.size(); ++i) {
  276.         valueMin = table[i];
  277.         j = i - 1;
  278.         while ((j >= 0) && (valueMin < table[j])) {
  279.             table[j + 1] = table[j];
  280.             j = j - 1;
  281.         }
  282.         table[j + 1] = valueMin;
  283.     }
  284. }
  285.  
  286.  
  287. void selectionSort(std::vector<int> &table) {
  288.     int indexMin, value_min;
  289.     for (int i = 0; i < (table.size() - 1); ++i) {
  290.         indexMin = i;
  291.         value_min = table[i];
  292.         for (int j = (i + 1); j < table.size(); ++j) {
  293.             if (table[j] < value_min) {
  294.                 indexMin = j;
  295.                 value_min = table[j];
  296.             }
  297.         }
  298.         std::swap(table[i], table[indexMin]);
  299.     }
  300. }
  301.  
  302.  
  303. void bubbleSort(std::vector<int> &table) {
  304.     for (int i = 0; i < table.size(); ++i) {
  305.         for (int j = 1; j < table.size(); ++j) {
  306.             if (table[j - 1] > table[j]) {
  307.                 std::swap(table[j - 1], table[j]);
  308.             }
  309.         }
  310.     }
  311. }
  312.  
  313. bool checkIfSorted(std::vector<int> &arr) {
  314.     int i = 1;
  315.     int is_sorted = 1;
  316.     while ((i < arr.size()) && is_sorted) {
  317.         if (arr[i - 1] > arr[i])
  318.             is_sorted = 0;
  319.         i++;
  320.     }
  321.     return static_cast<bool>(is_sorted);
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement