Advertisement
Tamjow

aads2

Apr 26th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 26.90 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <chrono>
  5. #include <string>
  6. #include <vector>
  7. #include <fstream>
  8. #include <iterator>
  9. #include <random>
  10.  
  11. using namespace std;
  12. int* selection_sort(int arr[], int sizearr)
  13. {
  14.     int smallest = 0;
  15.     int k = 0;
  16.     for (int i = 0; i < sizearr - 1; i++)
  17.     {
  18.         k = i;
  19.         smallest = arr[i];
  20.         for (int j = i + 1; j < sizearr; j++)
  21.         {
  22.             if (arr[j] < smallest) {
  23.                 k = j;
  24.                 smallest = arr[j];
  25.             }
  26.         }
  27.         arr[k] = arr[i];
  28.         arr[i] = smallest;
  29.     }
  30.     return arr;
  31. }
  32. void selectionstring(vector<string> &strings) {
  33.     typedef vector<string>::size_type size_type;
  34.     string smallest = "0";
  35.     int k = 0;
  36.     for (size_type i = 0; i < strings.size() - 1; i++) // for n-1 passes
  37.     {
  38.         k = i;
  39.         smallest = strings[i];
  40.         for (int j = i + 1; j < strings.size(); j++)
  41.         {
  42.             if (strings[j] < smallest) {
  43.                 k = j;
  44.                 smallest = strings[j];
  45.             }
  46.         }
  47.         strings[k] = strings[i];
  48.         strings[i] = smallest;
  49.     }
  50. }
  51.  
  52. int* bubble_sort(int arr[], int sizearr)
  53. {
  54.  
  55.     for (int i = 0; i < sizearr; i++) {
  56.         for (int j = 1; j < sizearr; j++) {
  57.             if (arr[j - 1] > arr[j]) {
  58.                 swap(arr[j - 1], arr[j]);
  59.             }
  60.         }
  61.     }
  62.     return arr;
  63. }
  64. void bubblestring(vector<string> &strings) {
  65.     typedef vector<string>::size_type size_type;
  66.     for (size_type i = 1; i < strings.size(); i++) // for n-1 passes
  67.     {
  68.         for (size_type j = 0; j < (strings.size() - 1); j++)
  69.         {
  70.             if (strings[j] > strings[j + 1])
  71.             {
  72.                 swap(strings[j], strings[j + 1]);
  73.             }
  74.         }
  75.     }
  76. }
  77.  
  78. int* bubble_sort2(int arr[], int sizearr) //check if changes were made
  79. {
  80.     int help = 0;
  81.     for (int i = 0; i < sizearr; i++) {
  82.         help = 0;
  83.         for (int j = 1; j < sizearr; j++) {
  84.             if (arr[j - 1] > arr[j]) {
  85.                 swap(arr[j - 1], arr[j]);
  86.                 help = 1; //swap has occured
  87.             }
  88.         }
  89.         if (help == 0) //if no swap occured in inner loop then stop
  90.             break;
  91.     }
  92.     return arr;
  93. }
  94. void bubblestring2(vector<string> &strings) ////check if changes were made
  95. {
  96.     typedef vector<string>::size_type size_type;
  97.     int help = 0;
  98.     for (size_type i = 1; i < strings.size(); i++) // for n-1 passes
  99.     {
  100.         help = 0;
  101.         for (size_type j = 0; j < (strings.size() - 1); j++)
  102.         {
  103.             if (strings[j] > strings[j + 1])
  104.             {
  105.                 swap(strings[j], strings[j + 1]);
  106.                 help = 1;
  107.             }
  108.         }
  109.         if (help == 0) //if no swap occured in inner loop then stop
  110.             break;
  111.     }
  112. }
  113.  
  114. int* bubble_sort3(int arr[], int sizearr) //(doesn't check the already sorted elements)
  115. {
  116.     int sorted = 0;
  117.     int i = 0, j = 1;
  118.  
  119.     while (sorted == 0) {
  120.         sorted = 1;
  121.         for (i = 0; i < sizearr - j; i++) {
  122.             if (arr[i] > arr[i + 1]) {
  123.                 swap(arr[i], arr[i + 1]);
  124.                 sorted = 0;
  125.             }
  126.         }
  127.         j++;
  128.     }
  129.     return arr;
  130. }
  131. void bubblestring3(vector<string> &strings) //(doesn't check the already sorted elements)
  132. {
  133.     typedef vector<string>::size_type size_type;
  134.     int sorted = 0;
  135.     int i = 0, j = 1;
  136.  
  137.     while (sorted == 0)
  138.     {
  139.         sorted = 1;
  140.         for (size_type i = 0; i < strings.size() - j; i++)
  141.         {
  142.             if (strings[i] > strings[i + 1])
  143.             {
  144.                 swap(strings[i], strings[i + 1]);
  145.                 sorted = 0;
  146.             }
  147.         }
  148.         j++;
  149.     }
  150. }
  151.  
  152. int* insertion_sort(int arr[], int sizearr)
  153. {
  154.     int x = 0;
  155.     int j = 0;
  156.     for (int i = 1; i < sizearr; i++) {
  157.         x = arr[i];
  158.         j = i - 1;
  159.         while (j >= 0 && x < arr[j]) {
  160.             arr[j + 1] = arr[j];
  161.             j = j - 1;
  162.         }
  163.         arr[j + 1] = x;
  164.     }
  165.     return arr;
  166. }
  167. void insertionstring(vector<string> &strings) {
  168.     typedef vector<string>::size_type size_type;
  169.     string x = "0";
  170.     int j = 0;
  171.     for (int i = 1; i < strings.size(); i++) {
  172.         x = strings[i];
  173.         j = i - 1;
  174.         while (j >= 0 && x < strings[j]) {
  175.             strings[j + 1] = strings[j];
  176.             j = j - 1;
  177.         }
  178.         strings[j + 1] = x;
  179.     }
  180. }
  181.  
  182. int* insertion_sort2(int arr[], int sizearr) //insertion with guard
  183. {
  184.     int smallest = arr[0];
  185.     int arr0 = arr[0];
  186.     for (int i = 1; i < sizearr; i++) { //check position of smallest element
  187.         if (arr[i] < smallest)
  188.             smallest = i;
  189.     }
  190.     swap(arr[0], arr[smallest]); //put smallest element at the zeroth array index
  191.     int x = 0;
  192.     int j = 0;
  193.     for (int i = 0; i < sizearr; i++) {
  194.         x = arr[i];
  195.         j = i - 1;
  196.         while (x <= arr[j]) {
  197.             arr[j + 1] = arr[j];
  198.             j--;
  199.         }
  200.         arr[j + 1] = x;
  201.     }
  202.     return arr;
  203. }
  204. void insertionstring2(vector<string> &strings) {
  205.     typedef vector<string>::size_type size_type;
  206.  
  207.     string smallest = "zzz";
  208.     int smallestidx = 0;
  209.     string arr0 = strings[0];
  210.     for (size_type i = 1; i < strings.size(); i++) { //check position of smallest element
  211.         if (strings[i] < strings[smallestidx])
  212.             smallestidx = i;
  213.     }
  214.     swap(strings[0], strings[smallestidx]); //put smallest element at the zeroth array index
  215.     string x = "zzz";
  216.     int j = 0;
  217.     for (size_type i = 1; i < strings.size(); i++) {
  218.         x = strings[i];
  219.         j = i - 1;
  220.         while (x <= strings[j]) {
  221.             strings[j + 1] = strings[j];
  222.             j--;
  223.         }
  224.         strings[j + 1] = x;
  225.     }
  226. }
  227.  
  228. int* shell1(int arr[], int sizearr)
  229. {
  230.     int h = 1;
  231.     int x, j;
  232.     while (h < sizearr / 9) {
  233.         h = 3 * h + 1;
  234.     }
  235.     while (h > 0) {
  236.         for (int i = h; i < sizearr; i++) {
  237.             x = arr[i];
  238.             j = i;
  239.             while (j >= h && x < arr[j - h]) {
  240.                 arr[j] = arr[j - h];
  241.                 j = j - h;
  242.             }
  243.             arr[j] = x;
  244.         }
  245.         h = h / 3;
  246.     }
  247.     return arr;
  248. }
  249. void shellstring(vector<string> &strings) //pivot is first value
  250. {
  251.     typedef vector<string>::size_type size_type;
  252.     int h = 1;
  253.     string x;
  254.     int j;
  255.     while (h < strings.size() / 9) {
  256.         h = 3 * h + 1;
  257.     }
  258.     while (h > 0) {
  259.         for (int i = h; i < strings.size(); i++) {
  260.             x = strings[i];
  261.             j = i;
  262.             while (j >= h && x < strings[j - h]) {
  263.                 strings[j] = strings[j - h];
  264.                 j = j - h;
  265.             }
  266.             strings[j] = x;
  267.         }
  268.         h = h / 3;
  269.     }
  270.  
  271. }
  272.  
  273. int* shell2(int arr[], int sizearr)
  274. {
  275.     int h = sizearr / 2;
  276.     int x, j;
  277.     while (h < sizearr) {
  278.         h = 3 * h + 1;
  279.     }
  280.     while (h > 0) {
  281.         for (int i = h; i < sizearr; i++) {
  282.             x = arr[i];
  283.             j = i;
  284.             while (j >= h && x < arr[j - h]) {
  285.                 arr[j] = arr[j - h];
  286.                 j = j - h;
  287.             }
  288.             arr[j] = x;
  289.         }
  290.         h = h / 2;
  291.     }
  292.     return arr;
  293. }
  294. void shellstring2(vector<string> &strings) //pivot is first value
  295. {
  296.     typedef vector<string>::size_type size_type;
  297.     int h = strings.size() / 2;
  298.     string x;
  299.     int j;
  300.     while (h < strings.size()) {
  301.         h = 3 * h + 1;
  302.     }
  303.     while (h > 0) {
  304.         for (size_type i = h; i < strings.size(); i++) {
  305.             x = strings[i];
  306.             j = i;
  307.             while (j >= h && x < strings[j - h]) {
  308.                 strings[j] = strings[j - h];
  309.                 j = j - h;
  310.             }
  311.             strings[j] = x;
  312.         }
  313.         h = h / 2;
  314.     }
  315.  
  316. }
  317.  
  318.  
  319. void insertion_qs(int arr[], int l, int r)
  320. {
  321.    
  322.     int x = 0;
  323.     int j = 0;
  324.     for (int i = l + 1; i < r; i++) {
  325.         x = arr[i];
  326.         j = i - 1;
  327.         while (j >= 0 && x < arr[j]) {
  328.             arr[j + 1] = arr[j];
  329.             j = j - 1;
  330.         }
  331.         arr[j + 1] = x;
  332.     }
  333. }
  334. void insertion_qsstring(vector<string> &strings, int l, int r)
  335. {
  336.     typedef vector<string>::size_type size_type;
  337.     string x = "0";
  338.     int j = 0;
  339.     for (size_type i = l + 1; i < r; i++) {
  340.         x = strings[i];
  341.         j = i - 1;
  342.         while (j >= 0 && x < strings[j]) {
  343.             strings[j + 1] = strings[j];
  344.             j = j - 1;
  345.         }
  346.         strings[j + 1] = x;
  347.     }
  348. }
  349.  
  350.  
  351.  
  352. void quicksort1(int arr[], int sizearr, int l, int r) //pivot is first element
  353. {
  354.     if (l < r) {
  355.         int pivot = 0;
  356.         int s = l;
  357.         if (l < r) {
  358.             pivot = arr[l];
  359.             s = l;
  360.             for (int i = l; i < r + 1; i++) {
  361.                 if (arr[i] < pivot) {
  362.                     s++;
  363.                     swap(arr[s], arr[i]);
  364.                 }
  365.             }
  366.             swap(arr[l], arr[s]);
  367.             quicksort1(arr, sizearr, l, s - 1);
  368.             quicksort1(arr, sizearr, s + 1, r);
  369.         }
  370.     }
  371.  
  372. }
  373. void quicksort1string(vector<string> &strings, int l, int r) //pivot is first element
  374. {
  375.     typedef vector<string>::size_type size_type;
  376.     if (l < r) {
  377.         string pivot = "0";
  378.         int s = l;
  379.         if (l < r) {
  380.             pivot = strings[l];
  381.             s = l;
  382.             for (int i = l; i < r + 1; i++) {
  383.                 if (strings[i] < pivot) {
  384.                     s++;
  385.                     swap(strings[s], strings[i]);
  386.                 }
  387.             }
  388.             swap(strings[l], strings[s]);
  389.             quicksort1string(strings, l, s - 1);
  390.             quicksort1string(strings, s + 1, r);
  391.         }
  392.     }
  393.  
  394. }
  395.  
  396. void quicksort2(int arr[], int sizearr, int l, int r) //pivot is a random element
  397. {
  398.  
  399.     int randn = rand() % sizearr;
  400.     swap(arr[randn], arr[0]);
  401.     if (l < r) {
  402.         int pivot = arr[0];
  403.         int s = l;
  404.         if (l < r) {
  405.             pivot = arr[0];
  406.             s = l;
  407.             for (int i = l; i < r; i++) {
  408.                 if (arr[i] < pivot) {
  409.                     s++;
  410.                     swap(arr[s], arr[i]);
  411.                 }
  412.             }
  413.             swap(arr[l], arr[s]);
  414.             quicksort1(arr, sizearr, l, s - 1);
  415.             quicksort1(arr, sizearr, s + 1, r);
  416.         }
  417.     }
  418.  
  419. }
  420. void quicksort2string(vector<string> &strings, int l, int r) //pivot is first element
  421. {
  422.     typedef vector<string>::size_type size_type;
  423.     if (l < r) {
  424.         string pivot;
  425.         int randn = rand() % strings.size();
  426.         swap(strings[randn], strings[0]);
  427.         int s = l;
  428.         if (l < r) {
  429.             pivot = strings[0];
  430.             s = l;
  431.             for (size_type i = l; i < r + 1; i++) {
  432.                 if (strings[i] < pivot) {
  433.                     s++;
  434.                     swap(strings[s], strings[i]);
  435.                 }
  436.             }
  437.             swap(strings[l], strings[s]);
  438.             quicksort2string(strings, l, s - 1);
  439.             quicksort2string(strings, s + 1, r);
  440.         }
  441.     }
  442.  
  443. }
  444.  
  445. void quicksort3(int arr[], int sizearr, int l, int r) //pivot is the median
  446. {
  447.     random_device rd;// obtain a random number from hardware
  448.     mt19937 eng(rd()); // seed the generator
  449.     uniform_int_distribution<> distr(0, sizearr - 1);
  450.     int rand1 = distr(eng);
  451.     int rand2 = distr(eng);
  452.     int rand3 = distr(eng);
  453.     int median = 0;
  454.     if (arr[rand1] <= arr[rand2] && arr[rand2] <= arr[rand3])
  455.         median = rand2;
  456.     if (arr[rand2] <= arr[rand1] && arr[rand1] <= arr[rand3])
  457.         median = rand1;
  458.     if (arr[rand1] <= arr[rand3] && arr[rand3] <= arr[rand2])
  459.         median = rand3;
  460.     swap(arr[median], arr[0]);
  461.     if (l < r) {
  462.         int pivot = arr[0];
  463.         int s = l;
  464.         if (l < r) {
  465.             pivot = arr[0];
  466.             s = l;
  467.             for (int i = l; i <= r; i++) {
  468.                 if (arr[i] < pivot) {
  469.                     s++;
  470.                     swap(arr[s], arr[i]);
  471.                 }
  472.             }
  473.             swap(arr[l], arr[s]);
  474.             quicksort1(arr, sizearr, l, s - 1);
  475.             quicksort1(arr, sizearr, s + 1, r);
  476.         }
  477.     }
  478.  
  479. }
  480. void quicksort3string(vector<string> &strings, int l, int r) //pivot is first element
  481. {
  482.     typedef vector<string>::size_type size_type;
  483.     if (l < r) {
  484.         string pivot = "0";
  485.         random_device rd;// obtain a random number from hardware
  486.         mt19937 eng(rd()); // seed the generator
  487.         uniform_int_distribution<> distr(0, strings.size() - 1);
  488.         int rand1 = distr(eng);
  489.         int rand2 = distr(eng);
  490.         int rand3 = distr(eng);
  491.         int median = 0;
  492.         if (strings[rand1] <= strings[rand2] && strings[rand2] <= strings[rand3])
  493.             median = rand2;
  494.         if (strings[rand2] <= strings[rand1] && strings[rand1] <= strings[rand3])
  495.             median = rand1;
  496.         if (strings[rand1] <= strings[rand3] && strings[rand3] <= strings[rand2])
  497.             median = rand3;
  498.         swap(strings[median], strings[0]);
  499.         int s = l;
  500.         if (l < r) {
  501.             pivot = strings[0];
  502.             s = l;
  503.             for (size_type i = l; i < r + 1; i++) {
  504.                 if (strings[i] < pivot) {
  505.                     s++;
  506.                     swap(strings[s], strings[i]);
  507.                 }
  508.             }
  509.             swap(strings[l], strings[s]);
  510.             quicksort3string(strings, l, s - 1);
  511.             quicksort3string(strings, s + 1, r);
  512.         }
  513.     }
  514.  
  515. }
  516.  
  517. void quicksortinsert(int arr[], int sizearr, int l, int r) //pivot is first element
  518. {
  519.     if (l < r) {
  520.         int pivot = 0;
  521.         int s = l;
  522.         if (l < r) {
  523.             if ((r - l) < 9) {
  524.                 insertion_qs(arr, l, r);
  525.             }
  526.             else {
  527.                 pivot = arr[l];
  528.                 s = l;
  529.                 for (int i = l; i < r + 1; i++) {
  530.                     if (arr[i] < pivot) {
  531.                         s++;
  532.                         swap(arr[s], arr[i]);
  533.                     }
  534.                 }
  535.                 swap(arr[l], arr[s]);
  536.                 quicksortinsert(arr, sizearr, l, s - 1);
  537.                 quicksortinsert(arr, sizearr, s + 1, r);
  538.             }
  539.         }
  540.  
  541.  
  542.     }
  543. }
  544. void quicksortinsertstring(vector<string> &strings, int l, int r) //pivot is first element
  545. {
  546.     typedef vector<string>::size_type size_type;
  547.     if (l < r) {
  548.         string pivot = "0";
  549.         int s = l;
  550.         if (l < r) {
  551.             if ((r - l) < 9) {
  552.                 insertion_qsstring(strings, l, r);
  553.             }
  554.             else {
  555.                 pivot = strings[l];
  556.                 s = l;
  557.                 for (int i = l; i < r + 1; i++) {
  558.                     if (strings[i] < pivot) {
  559.                         s++;
  560.                         swap(strings[s], strings[i]);
  561.                     }
  562.                 }
  563.                 swap(strings[l], strings[s]);
  564.                 quicksortinsertstring(strings,  l, s - 1);
  565.                 quicksortinsertstring(strings,  s + 1, r);
  566.             }
  567.         }
  568.  
  569.  
  570.     }
  571. }
  572.  
  573.  
  574. int check_if_sorted(int arr[], int sizearr) {
  575.     int i = 1;
  576.     int is_sorted = 1;
  577.     while ((i < sizearr) && is_sorted)
  578.     {
  579.         if (arr[i - 1] > arr[i])
  580.             is_sorted = 0;
  581.         i++;
  582.     }
  583.     return is_sorted;
  584. }
  585. int check_if_sortedstring(vector<string> &strings) {
  586.     int i = 1;
  587.     int is_sorted = 1;
  588.     while ((i < strings.size()) && is_sorted)
  589.     {
  590.         if (strings[i - 1] > strings[i]) {
  591.             is_sorted = 0;
  592.             cout << i << endl;
  593.         }
  594.         i++;
  595.     }
  596.    
  597.     return is_sorted;
  598. }
  599.  
  600. int main()
  601. {
  602.     const int size = 8000;
  603.     auto rng = default_random_engine{};
  604.     //int* array=new int[size];
  605.     int array[size];
  606.     for (int i = 0; i < size; i++) {
  607.         array[i] = i + 1;
  608.     }
  609.     random_shuffle(&array[0], &array[size]);
  610.  
  611.     vector<string> stringlist;
  612.     vector<double> exectimes;
  613.     ifstream fin("words32000.txt");
  614.     while (true) {
  615.         string s;
  616.         getline(fin, s);
  617.         if (fin.eof()) {
  618.             break;
  619.         }
  620.         stringlist.push_back(s);
  621.     }
  622.     fin.close();
  623.  
  624.  
  625.     //OUTPUT SORTED TO FILE, PICK ONE METHOD
  626.     //selectionstring(stringlist);
  627.     //insertionstring(stringlist);
  628.     //insertionstring2(stringlist);
  629.     //bubblestring(stringlist);
  630.     //bubblestring2(stringlist);
  631.     //bubblestring3(stringlist);
  632.     //shuffle(begin(stringlist), end(stringlist), rng);
  633.     //ofstream fout("output1024000.txt");
  634.     //copy(stringlist.begin(), stringlist.end(), ostream_iterator<string>(fout, "\n"));
  635.     //fout.close();
  636.     //cout << "completed randomizing" << endl;
  637.  
  638.  
  639.     //random_shuffle(&array[0], &array[size]);
  640.     ////SELECTION SORT
  641.     //cout << "random sorted?: " << check_if_sorted(array, size) << endl;
  642.     //auto start = chrono::high_resolution_clock::now();
  643.     //int *arr_sorted_selection = selection_sort(array, size);
  644.     //auto finish = chrono::high_resolution_clock::now();
  645.     //cout << "selection sorted?: " << check_if_sorted(arr_sorted_selection, size) << endl;
  646.     //chrono::duration<double> elapsed = finish - start;
  647.     //cout << "selection sort elapsed time: " << elapsed.count() << " s\n" << endl;
  648.  
  649.     //random_shuffle(&array[0], &array[size]);
  650.     ////BUBBLE SORT
  651.     //cout << "random sorted?: " << check_if_sorted(array, size) << endl;
  652.     //auto startb = chrono::high_resolution_clock::now();
  653.     //int *arr_sorted_bubble = bubble_sort(array, size);
  654.     //auto finishb = chrono::high_resolution_clock::now();
  655.     //cout << "bubble sorted?: " << check_if_sorted(arr_sorted_bubble, size) << endl;
  656.     //chrono::duration<double> elapsedb = finishb - startb;
  657.     //cout << "bubble sort elapsed time: " << elapsedb.count() << " s\n" << endl;
  658.  
  659.     //random_shuffle(&array[0], &array[size]);
  660.     ////INSERTION SORT
  661.     //cout << "random sorted?: " << check_if_sorted(array, size) << endl;
  662.     //auto startc = chrono::high_resolution_clock::now();
  663.     //int *arr_sorted_insertion = insertion_sort(array, size);
  664.     //auto finishc = chrono::high_resolution_clock::now();
  665.     //cout << "insertion sorted?: " << check_if_sorted(arr_sorted_insertion, size) << endl;
  666.     //chrono::duration<double> elapsedc = finishc - startc;
  667.     //cout << "insertion sort elapsed time: " << elapsedc.count() << " s\n" << endl;
  668.  
  669.     ////INSERTION SORT 2 (guard)
  670.     //random_shuffle(&array[0], &array[size]);
  671.     //cout << "random sorted?: " << check_if_sorted(array, size) << endl;
  672.     //auto startd = chrono::high_resolution_clock::now();
  673.     //int *arr_sorted_insertion2 = insertion_sort2(array, size);
  674.     //auto finishd = chrono::high_resolution_clock::now();
  675.     //cout << "insertion2 sorted?: " << check_if_sorted(arr_sorted_insertion2, size) << endl;
  676.     //chrono::duration<double> elapsedd = finishd - startd;
  677.     //cout << "insertion sort guard elapsed time: " << elapsedd.count() << " s\n" << endl;
  678.  
  679.     //random_shuffle(&array[0], &array[size]);
  680.     ////BUBBLE SORT 2 (check if any changes were made)
  681.     //cout << "random sorted?: " << check_if_sorted(array, size) << endl;
  682.     //auto startb2 = chrono::high_resolution_clock::now();
  683.     //int *arr_sorted_bubble2 = bubble_sort2(array, size);
  684.     //auto finishb2 = chrono::high_resolution_clock::now();
  685.     //cout << "bubble2 sorted?: " << check_if_sorted(arr_sorted_bubble2, size) << endl;
  686.     //chrono::duration<double> elapsedb2 = finishb2 - startb2;
  687.     //cout << "bubble2 sort elapsed time: " << elapsedb2.count() << " s\n" << endl;
  688.  
  689.     //random_shuffle(&array[0], &array[size]);
  690.     ////BUBBLE SORT 3 (doesn't check the already sorted elements)
  691.     //cout << "random sorted?: " << check_if_sorted(array, size) << endl;
  692.     //auto startb3 = chrono::high_resolution_clock::now();
  693.     //int *arr_sorted_bubble3 = bubble_sort3(array, size);
  694.     //auto finishb3 = chrono::high_resolution_clock::now();
  695.     //cout << "bubble3 sorted?: " << check_if_sorted(arr_sorted_bubble3, size) << endl;
  696.     //chrono::duration<double> elapsedb3 = finishb3 - startb3;
  697.     //cout << "bubble3 sort elapsed time: " << elapsedb3.count() << " s\n" << endl;
  698.  
  699.     ////SELECTION STRING SORT
  700.     //shuffle(begin(stringlist), end(stringlist), rng);
  701.     //cout << "randomly sorted?: " << check_if_sortedstring(stringlist) << endl;
  702.     //auto startss = chrono::high_resolution_clock::now();
  703.     //selectionstring(stringlist);
  704.     //auto finishss = chrono::high_resolution_clock::now();
  705.     //cout << "selection string sorted?: " << check_if_sortedstring(stringlist) << endl;
  706.     //chrono::duration<double> elapsedss = finishss - startss;
  707.     //cout << "selection string sort elapsed time: " << elapsedss.count() << " s\n" << endl;
  708.  
  709.     ////INSERTION STRING SORT
  710.     //shuffle(begin(stringlist), end(stringlist), rng);
  711.     //cout << "randomly sorted?: " << check_if_sortedstring(stringlist) << endl;
  712.     //auto startis = chrono::high_resolution_clock::now();
  713.     //insertionstring(stringlist);
  714.     //auto finishis = chrono::high_resolution_clock::now();
  715.     //cout << "insertion string sorted?: " << check_if_sortedstring(stringlist) << endl;
  716.     //chrono::duration<double> elapsedis = finishis - startis;
  717.     //cout << "insertion string elapsed time: " << elapsedis.count() << " s\n" << endl;
  718.  
  719.     ////INSERTION STRING2 SORT
  720.     //shuffle(begin(stringlist), end(stringlist), rng);
  721.     //cout << "randomly sorted?: " << check_if_sortedstring(stringlist) << endl;
  722.     //auto startis2 = chrono::high_resolution_clock::now();
  723.     //insertionstring2(stringlist);
  724.     //auto finishis2 = chrono::high_resolution_clock::now();
  725.     //cout << "insertion string 2 sorted?: " << check_if_sortedstring(stringlist) << endl;
  726.     //chrono::duration<double> elapsedis2 = finishis2 - startis2;
  727.     //cout << "insertion string 2 elapsed time: " << elapsedis2.count() << " s\n" << endl;
  728.  
  729.     ////BUBBLE STRING SORT
  730.     //shuffle(begin(stringlist), end(stringlist), rng);
  731.     //cout << "randomly sorted?: " << check_if_sortedstring(stringlist) << endl;
  732.     //auto startbs = chrono::high_resolution_clock::now();
  733.     //bubblestring(stringlist);
  734.     //auto finishbs = chrono::high_resolution_clock::now();
  735.     //cout << "bubble string sorted?: " << check_if_sortedstring(stringlist) << endl;
  736.     //chrono::duration<double> elapsedbs = finishbs - startbs;
  737.     //cout << "bubble string elapsed time: " << elapsedbs.count() << " s\n" << endl;
  738.  
  739.     ////BUBBLE STRING2 SORT
  740.     //shuffle(begin(stringlist), end(stringlist), rng);
  741.     //cout << "randomly sorted?: " << check_if_sortedstring(stringlist) << endl;
  742.     //auto startbs2 = chrono::high_resolution_clock::now();
  743.     //bubblestring2(stringlist);
  744.     //auto finishbs2 = chrono::high_resolution_clock::now();
  745.     //cout << "bubble string 2 sorted?: " << check_if_sortedstring(stringlist) << endl;
  746.     //chrono::duration<double> elapsedbs2 = finishbs2 - startbs2;
  747.     //cout << "bubble string 2 elapsed time: " << elapsedbs2.count() << " s\n" << endl;
  748.  
  749.     ////BUBBLE STRING3 SORT (doesn't check the already sorted elements)
  750.     //shuffle(begin(stringlist), end(stringlist), rng);
  751.     //cout << "randomly sorted?: " << check_if_sortedstring(stringlist) << endl;
  752.     //auto startbs3 = chrono::high_resolution_clock::now();
  753.     //bubblestring3(stringlist);
  754.     //auto finishbs3 = chrono::high_resolution_clock::now();
  755.     //cout << "bubble string 3 sorted?: " << check_if_sortedstring(stringlist) << endl;
  756.     //chrono::duration<double> elapsedbs3 = finishbs3 - startbs3;
  757.     //cout << "bubble string 3 elapsed time: " << elapsedbs3.count() << " s\n" << endl;
  758.  
  759.    
  760.    
  761.  
  762.     ////QS1 SORT
  763.     //random_shuffle(&array[0], &array[size]);
  764.     //cout << "random sorted?: " << check_if_sorted(array, size) << endl;
  765.     //auto startqs1 = chrono::high_resolution_clock::now();
  766.     //quicksort1(array, size, 0, size - 1);
  767.     //auto finishqs1 = chrono::high_resolution_clock::now();
  768.     //cout << "qs1 sorted?: " << check_if_sorted(array, size) << endl;
  769.     //chrono::duration<double> elapsedqs1 = finishqs1 - startqs1;
  770.     //cout << "qs1 sort elapsed time: " << elapsedqs1.count() << " s\n" << endl;
  771.  
  772.  
  773.     ////QS2 SORT
  774.     //random_shuffle(&array[0], &array[size]);
  775.     //cout << "random sorted?: " << check_if_sorted(array, size) << endl;
  776.     //auto startqs2 = chrono::high_resolution_clock::now();
  777.     //quicksort2(array, size, 0, size - 1);
  778.     //auto finishqs2 = chrono::high_resolution_clock::now();
  779.     //cout << "qs2 sorted?: " << check_if_sorted(array, size) << endl;
  780.     //chrono::duration<double> elapsedqs2 = finishqs2 - startqs2;
  781.     //cout << "qs2 sort elapsed time: " << elapsedqs2.count() << " s\n" << endl;
  782.  
  783.  
  784.     ////QS3 SORT
  785.     //random_shuffle(&array[0], &array[size]);
  786.     //cout << "random sorted?: " << check_if_sorted(array, size) << endl;
  787.     //auto startqs3 = chrono::high_resolution_clock::now();
  788.     //quicksort3(array, size, 0, size - 1);
  789.     //auto finishqs3 = chrono::high_resolution_clock::now();
  790.     //cout << "qs3 sorted?: " << check_if_sorted(array, size) << endl;
  791.     //chrono::duration<double> elapsedqs3 = finishqs3 - startqs3;
  792.     //cout << "qs3 sort elapsed time: " << elapsedqs3.count() << " s\n" << endl;
  793.  
  794.     ////QSINSERT SORT
  795.     //random_shuffle(&array[0], &array[size]);
  796.     //cout << "random sorted?: " << check_if_sorted(array, size) << endl;
  797.     //auto startqs4 = chrono::high_resolution_clock::now();
  798.     //quicksortinsert(array, size, 0, size - 1);
  799.     //auto finishqs4 = chrono::high_resolution_clock::now();
  800.     //cout << "qs+insertion sorted?: " << check_if_sorted(array, size) << endl;
  801.     //chrono::duration<double> elapsedqs4 = finishqs4 - startqs4;
  802.     //cout << "qs+insertion sort elapsed time: " << elapsedqs4.count() << " s\n" << endl;
  803.  
  804.     ////SHELL1 SORT
  805.     //random_shuffle(&array[0], &array[size]);
  806.     //cout << "random sorted?: " << check_if_sorted(array, size) << endl;
  807.     //auto startsh1 = chrono::high_resolution_clock::now();
  808.     //int *arr_sorted_shell1 = shell1(array, size);
  809.     //auto finishsh1 = chrono::high_resolution_clock::now();
  810.     //cout << "shell1 sorted?: " << check_if_sorted(arr_sorted_shell1, size) << endl;
  811.     //chrono::duration<double> elapsedsh1 = finishsh1 - startsh1;
  812.     //cout << "shell1 sort elapsed time: " << elapsedsh1.count() << " s\n" << endl;
  813.  
  814.  
  815.  
  816.     ////SHELL2 SORT
  817.     //random_shuffle(&array[0], &array[size]);
  818.     //cout << "random sorted?: " << check_if_sorted(array, size) << endl;
  819.     //auto startsh2 = chrono::high_resolution_clock::now();
  820.     //int *arr_sorted_shell2 = shell2(array, size);
  821.     //auto finishsh2 = chrono::high_resolution_clock::now();
  822.     //cout << "shell2 sorted?: " << check_if_sorted(arr_sorted_shell2, size) << endl;
  823.     //chrono::duration<double> elapsedsh2 = finishsh2 - startsh2;
  824.     //cout << "shell2 sort elapsed time: " << elapsedsh2.count() << " s\n" << endl;
  825.  
  826.     ////SHELL1 STRING SORT
  827.     //shuffle(begin(stringlist), end(stringlist), rng);
  828.     //cout << "randomly sorted?: " << check_if_sortedstring(stringlist) << endl;
  829.     //auto startsh1s = chrono::high_resolution_clock::now();
  830.     //shellstring(stringlist);
  831.     //auto finishsh1s = chrono::high_resolution_clock::now();
  832.     //cout << "shell1 string sorted?: " << check_if_sortedstring(stringlist) << endl;
  833.     //chrono::duration<double> elapsedsh1s = finishsh1s - startsh1s;
  834.     //cout << "shell1 string elapsed time: " << elapsedsh1s.count() << " s\n" << endl;
  835.  
  836.     ////SHELL2 STRING SORT
  837.     //shuffle(begin(stringlist), end(stringlist), rng);
  838.     //cout << "randomly sorted?: " << check_if_sortedstring(stringlist) << endl;
  839.     //auto startsh2s = chrono::high_resolution_clock::now();
  840.     //shellstring2(stringlist);
  841.     //auto finishsh2s = chrono::high_resolution_clock::now();
  842.     //cout << "shell2 string sorted?: " << check_if_sortedstring(stringlist) << endl;
  843.     //chrono::duration<double> elapsedsh2s = finishsh2s - startsh2s;
  844.     //cout << "shell2 string elapsed time: " << elapsedsh2s.count() << " s\n" << endl;
  845.  
  846.     //QS1 STRING SORT
  847.     shuffle(begin(stringlist), end(stringlist), rng);
  848.     cout << "randomly sorted?: " << check_if_sortedstring(stringlist) << endl;
  849.     auto startqs1s = chrono::high_resolution_clock::now();
  850.     quicksort1string(stringlist, 0, stringlist.size() - 1);
  851.     auto finishqs1s = chrono::high_resolution_clock::now();
  852.     cout << "QS1 string sorted?: " << check_if_sortedstring(stringlist) << endl;
  853.     chrono::duration<double> elapsedqs1s = finishqs1s - startqs1s;
  854.     cout << "QS1 string elapsed time: " << elapsedqs1s.count() << " s\n" << endl;
  855.  
  856.     //QS2 STRING SORT
  857.     shuffle(begin(stringlist), end(stringlist), rng);
  858.     cout << "randomly sorted?: " << check_if_sortedstring(stringlist) << endl;
  859.     auto startqs2s = chrono::high_resolution_clock::now();
  860.     quicksort2string(stringlist, 0, stringlist.size() - 1);
  861.     auto finishqs2s = chrono::high_resolution_clock::now();
  862.     cout << "QS2 string sorted?: " << check_if_sortedstring(stringlist) << endl;
  863.     chrono::duration<double> elapsedqs2s = finishqs2s - startqs2s;
  864.     cout << "QS2 string elapsed time: " << elapsedqs2s.count() << " s\n" << endl;
  865.  
  866.     ////QS3 STRING SORT
  867.     //shuffle(begin(stringlist), end(stringlist), rng);
  868.     //cout << "randomly sorted?: " << check_if_sortedstring(stringlist) << endl;
  869.     //auto startqs3s = chrono::high_resolution_clock::now();
  870.     //quicksort3string(stringlist, 0, stringlist.size() - 1);
  871.     //auto finishqs3s = chrono::high_resolution_clock::now();
  872.     //cout << "QS3 string sorted?: " << check_if_sortedstring(stringlist) << endl;
  873.     //chrono::duration<double> elapsedqs3s = finishqs3s - startqs3s;
  874.     //cout << "QS3 string elapsed time: " << elapsedqs3s.count() << " s\n" << endl;
  875.  
  876.     //QS+INSERT STRING SORT
  877.     shuffle(begin(stringlist), end(stringlist), rng);
  878.     cout << "randomly sorted?: " << check_if_sortedstring(stringlist) << endl;
  879.     auto startqs4s = chrono::high_resolution_clock::now();
  880.     quicksortinsertstring(stringlist, 0, stringlist.size() - 1);
  881.     auto finishqs4s = chrono::high_resolution_clock::now();
  882.     cout << "QS + insert string sorted?: " << check_if_sortedstring(stringlist) << endl;
  883.     chrono::duration<double> elapsedqs4s = finishqs4s - startqs4s;
  884.     cout << "QS + insert string elapsed time: " << elapsedqs4s.count() << " s\n" << endl;
  885.  
  886.  
  887.  
  888. //  exectimes.push_back(elapsedqs1s.count());
  889. //  exectimes.push_back(elapsedqs2s.count());
  890. //  exectimes.push_back(elapsedqs3s.count());
  891. //  exectimes.push_back(elapsedqs4s.count());
  892. //  exectimes.push_back(elapsedsh1s.count());
  893. //  exectimes.push_back(elapsedsh2s.count());
  894.    
  895.  
  896.     vector<string> doubleStr;
  897.     transform(exectimes.begin(),
  898.         exectimes.end(),
  899.         back_inserter(doubleStr),
  900.         [](double d) { return to_string(d); }
  901.     );
  902.  
  903.     //ofstream fout("times.txt");
  904.     //copy(timesstr.begin(), timesstr.end(), ostream_iterator<string>(fout, "\n"));
  905.     //fout.close();
  906.  
  907.     ofstream output_file("times.txt");
  908.     ostream_iterator<string> output_iterator(output_file, "\n");
  909.     copy(doubleStr.begin(), doubleStr.end(), output_iterator);
  910.     output_file.close();
  911.     system("pause");
  912.  
  913.     return 0;
  914. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement