Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.70 KB | None | 0 0
  1. /************************************************************/
  2. // System includes
  3.  
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <locale.h>
  7. #include <cstdlib>
  8. #include <vector>
  9. #include <string>
  10. #include <random>
  11. #include <algorithm>
  12. #include <queue>
  13. #include <array>
  14.  
  15. /************************************************************/
  16. // Local includes
  17.  
  18. #include "Timer.hpp"
  19. /************************************************************/
  20. // Using declarations
  21.  
  22. using std::cout;
  23. using std::cin;
  24. using std::endl;
  25. using std::vector;
  26. using std::string;
  27. using std::minstd_rand;
  28. using std::swap;
  29. using std::sort;
  30. using std::queue;
  31. using std::array;
  32.  
  33. /************************************************************/
  34. // Function prototypes/global vars/typedefs
  35.  
  36. void
  37. printIntro (size_t& numElem, int& digits);
  38.  
  39. void
  40. printOutro (vector<int>& A, vector<int>& Acopy);
  41.  
  42. void
  43. populateVector (vector<int>& A, size_t& numElem, int& digits);
  44.  
  45. void
  46. radixSort (vector<int>& A, int& digitPos);
  47.  
  48. /*size_t
  49. insertionSort (vector<int>& v);*/
  50.  
  51. /************************************************************/
  52.  
  53. int    
  54. main (int argc, char* argv[])
  55. {
  56.     vector<int> A;
  57.     size_t numElem;
  58.     int digits;
  59.     int digitPos;
  60.  
  61.     Timer<> timer;
  62.     Timer<> timer2;
  63.     timer.start();
  64.     printIntro (numElem, digits);
  65.     populateVector (A, numElem, digits);
  66.     vector<int> Acopy(A);
  67.     cout << "\nAcopy: ";
  68.     for (int val: Acopy)
  69.     {
  70.         cout << val << " | ";
  71.     }
  72.     timer2.start();
  73.     sort (Acopy.begin(), Acopy.end());
  74.     timer2.stop();
  75.     cout << "\nSorted Acopy: ";
  76.     for (int val: Acopy)
  77.     {
  78.         cout << val << " | ";
  79.     }
  80.     cout << endl;
  81.     digitPos = 1;
  82.     radixSort (A, digitPos);
  83.     digitPos = 10;
  84.     radixSort (A, digitPos);
  85.     timer.stop();
  86.  
  87.     printOutro (A, Acopy);
  88.  
  89.     cout << "Radix time:     " << timer.getElapsedMs() << " ms" <<  endl;
  90.     cout << "std::sort time: " << timer2.getElapsedMs() << " ms" <<  endl;
  91.  
  92.  
  93.     return EXIT_SUCCESS;
  94. }
  95.  
  96. /************************************************************/
  97.  
  98. void
  99. printIntro (size_t& numElem, int& digits)
  100. {
  101.    cout << "N ==> ";
  102.    cin >> numElem;
  103.    numElem = numElem;
  104.  
  105.  
  106.    cout << "d ==> ";
  107.    cin >> digits;
  108.    digits = digits;
  109.  
  110.    cout << endl;
  111. }
  112.  
  113. /************************************************************/
  114.  
  115. void
  116. printOutro (vector<int>& A, vector<int>& Acopy)
  117. {
  118.      if (A == Acopy)
  119.     {
  120.         cout << "success\n";
  121.     }
  122.     else
  123.     {
  124.         cout << "fail\n";
  125.     }
  126. }
  127.  
  128. /************************************************************/
  129.  
  130. // Takes the given inputs from the user, to fill vector 'v'.
  131. // Then calls the selected sort using the filled vector.
  132. void
  133. populateVector (vector<int>& A, size_t& numElem, int& digits)
  134. {
  135.     minstd_rand gen;
  136.     gen.seed(0);
  137.     std::uniform_int_distribution<int> dist(0, pow(10, digits));
  138.     //fill(A.begin(), A.end(), dist(gen));
  139.     do {
  140.         A.push_back(dist(gen));
  141.     } while (A.size() != numElem) ;
  142.     cout << endl;
  143.    
  144.  
  145. }
  146.  
  147. /************************************************************/
  148.  
  149. // Takes the vector created by the users.
  150. // Then runs the sort using an array of 10 queues.
  151. void
  152. radixSort (vector<int>& A, int& digitPos)
  153. {
  154.     int position;
  155.     queue<int> holder[10];
  156.     for(int val: A)
  157.     {
  158.         if(digitPos == 1)
  159.             position = val % 10;
  160.  
  161.         if (digitPos == 10)
  162.             position = (val / 10) % 10;
  163.  
  164.         holder[position].push(val);
  165.         cout << "pushed: " << val << " onto queue\n";
  166.     }
  167.    
  168.     int n = 0;
  169.     cout << "A: ";
  170.     for(int val: A)
  171.     {
  172.         cout << val << " | ";
  173.     }
  174.     cout << endl;
  175.     A.clear();
  176.  
  177.     while(!holder[0].empty())
  178.     {
  179.         A.push_back(holder[0].front());
  180.         cout << "pushed: " << holder[0].front() << endl;
  181.         holder[0].pop();
  182.         ++n;
  183.     }
  184.     while(!holder[1].empty())
  185.     {
  186.         A.push_back(holder[1].front());
  187.         cout << "pushed: " << holder[1].front() << endl;
  188.         holder[1].pop();
  189.         ++n;
  190.     }
  191.     while(!holder[2].empty())
  192.     {
  193.         A.push_back(holder[2].front());
  194.         cout << "pushed: " << holder[2].front() << endl;
  195.         holder[2].pop();
  196.         ++n;
  197.     }
  198.     while(!holder[3].empty())
  199.     {
  200.         A.push_back(holder[3].front());
  201.         cout << "pushed: " << holder[3].front() << endl;
  202.         holder[3].pop();
  203.         ++n;
  204.     }
  205.     while(!holder[4].empty())
  206.     {
  207.         A.push_back(holder[4].front());
  208.         cout << "pushed: " << holder[4].front() << endl;
  209.         holder[4].pop();
  210.         ++n;
  211.     }
  212.     while(!holder[5].empty())
  213.     {
  214.         A.push_back(holder[5].front());
  215.         cout << "pushed: " << holder[5].front() << endl;
  216.         holder[5].pop();
  217.         ++n;
  218.     }
  219.     while(!holder[6].empty())
  220.     {
  221.         A.push_back(holder[6].front());
  222.         cout << "pushed: " << holder[6].front() << endl;
  223.         holder[6].pop();
  224.         ++n;
  225.     }
  226.     while(!holder[7].empty())
  227.     {
  228.         A.push_back(holder[7].front());
  229.         cout << "pushed: " << holder[7].front() << endl;
  230.         holder[7].pop();
  231.         ++n;
  232.     }
  233.     while(!holder[8].empty())
  234.     {
  235.         A.push_back(holder[8].front());
  236.         cout << "pushed: " << holder[8].front() << endl;
  237.         holder[8].pop();
  238.         ++n;
  239.     }
  240.     while(!holder[9].empty())
  241.     {
  242.         A.push_back(holder[9].front());
  243.         cout << "pushed: " << holder[9].front() << endl;
  244.         holder[9].pop();
  245.         ++n;
  246.     }
  247.  
  248.    
  249.     for (int val: A)
  250.     {
  251.         cout << val << " | ";
  252.     }
  253.     cout << endl;
  254. }
  255.  
  256. /************************************************************/
  257. /************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement