Advertisement
Sanlover

Untitled

Dec 18th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.92 KB | None | 0 0
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <random>
  4. #include <vector>
  5.  
  6. using std::cout;
  7. using std::endl;
  8. using std::setw;
  9. using std::swap;
  10. using std::vector;
  11.  
  12. const size_t kSpacingSize = 36;
  13. const size_t kFirstSize = 100;
  14. const size_t kSecondSize = 1000;
  15. const int kLeftNumRange = 0;
  16. const int kRightNumRange = 1000;
  17.  
  18. void quick_sort(vector<int>& _Container, int _Left, int _Right, size_t& _Iterations, size_t& _Compares)
  19. {
  20.     _Iterations++;
  21.     int i = _Left, j = _Right;
  22.  
  23.     int tmp;
  24.  
  25.     int pivot = _Container[(_Left + _Right) / 2];
  26.  
  27.     _Compares++;
  28.     while (i <= j)
  29.     {
  30.         _Compares++;
  31.         while (_Container[i] < pivot)
  32.         {
  33.             i++;
  34.             _Compares++;
  35.         }
  36.         _Compares++;
  37.         while (_Container[j] > pivot)
  38.         {
  39.             j--;
  40.             _Compares++;
  41.         }
  42.  
  43.         _Compares++;
  44.         if (i <= j)
  45.         {
  46.             tmp = _Container[i];
  47.             _Container[i] = _Container[j];
  48.             _Container[j] = tmp;
  49.             i++;
  50.             j--;
  51.         }
  52.     };
  53.     _Compares++;
  54.     if (_Left < j)
  55.         quick_sort(_Container, _Left, j, _Iterations, _Compares);
  56.  
  57.     _Compares++;
  58.     if (i < _Right)
  59.         quick_sort(_Container, i, _Right, _Iterations, _Compares);
  60. }
  61.  
  62. void heap_method(vector<int>& _Container, const size_t& _Size, const size_t& _Pos, size_t& _Iterations,
  63.     size_t& _Compares)
  64. {
  65.     _Iterations++;
  66.     size_t largest = _Pos;
  67.     const size_t left = 2 * _Pos + 1;
  68.     const size_t right = 2 * _Pos + 2;
  69.  
  70.     if (left < _Size && _Container[left] > _Container[largest])
  71.     {
  72.         largest = left;
  73.         _Compares++;
  74.     }
  75.     if (right < _Size && _Container[right] > _Container[largest])
  76.     {
  77.         largest = right;
  78.         _Compares++;
  79.     }
  80.     if (largest != _Pos)
  81.     {
  82.         swap(_Container[_Pos], _Container[largest]);
  83.         _Compares++;
  84.         heap_method(_Container, _Size, largest, _Iterations, _Compares);
  85.     }
  86. }
  87.  
  88. void heap_sort(vector<int>& _Container, const size_t& _Size, size_t& _Iterations, size_t& _Compares)
  89. {
  90.     for (int i = static_cast<int>(_Size) / 2 - 1; i >= 0; i--)
  91.     {
  92.         heap_method(_Container, _Size, i, _Iterations, _Compares);
  93.     }
  94.     for (int i = static_cast<int>(_Size) - 1; i >= 0; i--)
  95.     {
  96.         swap(_Container[0], _Container[i]);
  97.  
  98.         heap_method(_Container, i, 0, _Iterations, _Compares);
  99.     }
  100. }
  101.  
  102. void bubble_sort(vector<int>& _Container, size_t& _Iterations, size_t& _Compares)
  103. {
  104.     for (size_t i = 0; i < _Container.size() - 1; i++)
  105.     {
  106.         bool is_swapped = false;
  107.         for (size_t j = 0; j < _Container.size() - i - 1; j++)
  108.         {
  109.             _Iterations++;
  110.             if (_Container[j] > _Container[j + 1])
  111.             {
  112.                 _Compares++;
  113.                 swap(_Container[j], _Container[j + 1]);
  114.                 is_swapped = true;
  115.             }
  116.         }
  117.  
  118.         if (!is_swapped)
  119.             break;
  120.     }
  121. }
  122.  
  123. int main()
  124. {
  125.     std::random_device seed;
  126.     std::mt19937 generator(seed());
  127.     const std::uniform_int_distribution<> random_result(kLeftNumRange, kRightNumRange);
  128.  
  129.     vector<int> test(kFirstSize);
  130.     vector<int> test_s(kSecondSize);
  131.     vector<int> test1(kFirstSize);
  132.     vector<int> test1_s(kSecondSize);
  133.     vector<int> test2(kFirstSize);
  134.     vector<int> test2_s(kSecondSize);
  135.  
  136.     for (size_t i = 0; i < kFirstSize; i++)
  137.     {
  138.         test[i] = random_result(generator);
  139.         test1[i] = random_result(generator);
  140.         test2[i] = random_result(generator);
  141.     }
  142.     for (size_t i = 0; i < kSecondSize; i++)
  143.     {
  144.         test_s[i] = random_result(generator);
  145.         test1_s[i] = random_result(generator);
  146.         test2_s[i] = random_result(generator);
  147.     }
  148.  
  149.     //..................... Bubble Sort
  150.  
  151.     cout << "######################################### BUBBLE SORT #########################################" << endl;
  152.  
  153.     size_t iterations = 0, compares = 0;
  154.  
  155.     // 100
  156.     cout << "100 numbers BEFORE Sort: " << endl
  157.         << "----------------------------------------------------------------------------------------------";
  158.     for (size_t it = 0; it < test.size(); it++)
  159.     {
  160.         if (it % kSpacingSize == 0)
  161.             cout << endl;
  162.         cout << setw(4) << test[it] << " ";
  163.     }
  164.     bubble_sort(test, iterations, compares);
  165.  
  166.     cout << endl
  167.         << "----------------------------------------------------------------------------------------------" << endl;
  168.     cout << endl
  169.         << "100 numbers AFTER Sort : " << endl
  170.         << "----------------------------------------------------------------------------------------------";
  171.     for (size_t it = 0; it < test.size(); it++)
  172.     {
  173.         if (it % kSpacingSize == 0)
  174.             cout << endl;
  175.         cout << setw(4) << test[it] << " ";
  176.     }
  177.     cout << endl
  178.         << "----------------------------------------------------------------------------------------------" << endl;
  179.     cout << "RESULTS: Iterations = " << iterations << " | Compares = " << compares << endl << endl;
  180.  
  181.     // 1000
  182.     iterations = compares = 0;
  183.     cout << "1000 numbers BEFORE Sort: " << endl
  184.         << "----------------------------------------------------------------------------------------------";
  185.     for (size_t it = 0; it < test_s.size(); it++)
  186.     {
  187.         if (it % kSpacingSize == 0)
  188.             cout << endl;
  189.         cout << setw(4) << test_s[it] << " ";
  190.     }
  191.     bubble_sort(test_s, iterations, compares);
  192.  
  193.     cout << endl
  194.         << "----------------------------------------------------------------------------------------------" << endl;
  195.     cout << endl
  196.         << "1000 numbers AFTER Sort : " << endl
  197.         << "----------------------------------------------------------------------------------------------";
  198.     for (size_t it = 0; it < test_s.size(); it++)
  199.     {
  200.         if (it % kSpacingSize == 0)
  201.             cout << endl;
  202.         cout << setw(4) << test_s[it] << " ";
  203.     }
  204.     cout << endl
  205.         << "----------------------------------------------------------------------------------------------" << endl;
  206.     cout << "RESULTS: Iterations = " << iterations << " | Compares = " << compares << endl << endl;
  207.  
  208.     //..................... Heap Sort
  209.  
  210.     iterations = compares = 0;
  211.     // 100
  212.     cout << "########################################## HEAP SORT ##########################################" << endl;
  213.     cout << endl
  214.         << "100 numbers BEFORE Sort: " << endl
  215.         << "----------------------------------------------------------------------------------------------";
  216.     for (size_t it = 0; it < test1.size(); it++)
  217.     {
  218.         if (it % kSpacingSize == 0)
  219.             cout << endl;
  220.         cout << setw(4) << test1[it] << " ";
  221.     }
  222.     heap_sort(test1, test1.size(), iterations, compares);
  223.  
  224.     cout << endl
  225.         << "----------------------------------------------------------------------------------------------" << endl;
  226.     cout << endl
  227.         << "100 numbers AFTER Sort : " << endl
  228.         << "----------------------------------------------------------------------------------------------";
  229.     for (size_t it = 0; it < test1.size(); it++)
  230.     {
  231.         if (it % kSpacingSize == 0)
  232.             cout << endl;
  233.         cout << setw(4) << test1[it] << " ";
  234.     }
  235.     cout << endl
  236.         << "----------------------------------------------------------------------------------------------" << endl;
  237.     cout << "RESULTS: Iterations = " << iterations << " | Compares = " << compares << endl << endl;
  238.  
  239.     iterations = compares = 0;
  240.  
  241.     // 1000
  242.     cout << endl
  243.         << "1000 numbers BEFORE Sort: " << endl
  244.         << "----------------------------------------------------------------------------------------------";
  245.     for (size_t it = 0; it < test1_s.size(); it++)
  246.     {
  247.         if (it % kSpacingSize == 0)
  248.             cout << endl;
  249.         cout << setw(4) << test1_s[it] << " ";
  250.     }
  251.     heap_sort(test1_s, test1_s.size(), iterations, compares);
  252.  
  253.     cout << endl
  254.         << "----------------------------------------------------------------------------------------------" << endl;
  255.     cout << endl
  256.         << "1000 numbers AFTER Sort : " << endl
  257.         << "----------------------------------------------------------------------------------------------";
  258.     for (size_t it = 0; it < test1_s.size(); it++)
  259.     {
  260.         if (it % kSpacingSize == 0)
  261.             cout << endl;
  262.         cout << setw(4) << test1_s[it] << " ";
  263.     }
  264.     cout << endl
  265.         << "----------------------------------------------------------------------------------------------" << endl;
  266.     cout << "RESULTS: Iterations = " << iterations << " | Compares = " << compares << endl << endl;
  267.  
  268.     //..................... Quick Sort
  269.  
  270.     iterations = compares = 0;
  271.  
  272.     // 100
  273.     cout << "########################################## QUICK SORT #########################################" << endl;
  274.     cout << endl
  275.         << "100 numbers BEFORE Sort: " << endl
  276.         << "----------------------------------------------------------------------------------------------";
  277.     for (size_t it = 0; it < test2.size(); it++)
  278.     {
  279.         if (it % kSpacingSize == 0)
  280.             cout << endl;
  281.         cout << setw(4) << test2[it] << " ";
  282.     }
  283.     quick_sort(test2, 0, test2.size() - 1, iterations, compares);
  284.  
  285.     cout << endl
  286.         << "----------------------------------------------------------------------------------------------" << endl;
  287.     cout << endl
  288.         << "100 numbers AFTER Sort: " << endl
  289.         << "----------------------------------------------------------------------------------------------";
  290.     for (size_t it = 0; it < test2.size(); it++)
  291.     {
  292.         if (it % kSpacingSize == 0)
  293.             cout << endl;
  294.         cout << setw(4) << test2[it] << " ";
  295.     }
  296.     cout << endl
  297.         << "----------------------------------------------------------------------------------------------" << endl;
  298.     cout << "RESULTS: Iterations = " << iterations << " | Compares = " << compares << endl << endl;
  299.  
  300.     iterations = compares = 0;
  301.     // 1000
  302.  
  303.     cout << endl
  304.         << "1000 numbers BEFORE Sort: " << endl
  305.         << "----------------------------------------------------------------------------------------------";
  306.     for (size_t it = 0; it < test2_s.size(); it++)
  307.     {
  308.         if (it % kSpacingSize == 0)
  309.             cout << endl;
  310.         cout << setw(4) << test2_s[it] << " ";
  311.     }
  312.     quick_sort(test2_s, 0, test2_s.size() - 1, iterations, compares);
  313.  
  314.     cout << endl
  315.         << "----------------------------------------------------------------------------------------------" << endl;
  316.     cout << endl
  317.         << "1000 numbers AFTER Sort: " << endl
  318.         << "----------------------------------------------------------------------------------------------";
  319.     for (size_t it = 0; it < test2_s.size(); it++)
  320.     {
  321.         if (it % kSpacingSize == 0)
  322.             cout << endl;
  323.         cout << setw(4) << test2_s[it] << " ";
  324.     }
  325.     cout << endl
  326.         << "----------------------------------------------------------------------------------------------" << endl;
  327.     cout << "RESULTS: Iterations = " << iterations << " | Compares = " << compares << endl << endl;
  328.     return 0;
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement