Advertisement
M3IY0U

Untitled

Dec 2nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <array>
  4. #include <string>
  5. #include <cstdlib>
  6.  
  7. using namespace std;
  8. enum ContainerType { VECTOR, ARRAY };
  9.  
  10. template <typename T>
  11. void printContainer(T);
  12. template <typename T>
  13. void bubbleSort(T&);
  14. template <typename T>
  15. void swap(T&, int, int);
  16. template <typename T>
  17. void selectionSort(T&);
  18. template <typename T>
  19. bool isLessEqualSorted(T);
  20. template <typename T>
  21. string isSorted(T);
  22. template <typename T>
  23. T randomStringContainer(const ContainerType, const int, const int);
  24.  
  25.  
  26.  
  27. #define NAME_OF(v) #v
  28.  
  29.  
  30. int main() {
  31.     vector <int> vector1 = randomStringContainer(VECTOR,10,4);
  32.     vector <int> vector2 = { 7,6,5,4,3,2,1,0 };
  33.     printContainer(vector1);
  34.     bubbleSort(vector1);
  35.     printContainer(vector1);
  36.     cout << NAME_OF(vector1) << isSorted(vector1);
  37.     cout << endl;
  38.     printContainer(vector2);
  39.     selectionSort(vector2);
  40.     printContainer(vector2);
  41.     cout << NAME_OF(vector2) << isSorted(vector2);
  42. }
  43.  
  44.  
  45. template <typename T>
  46. void bubbleSort(T& v) {
  47.     for (auto i = 0; i < v.size(); i++) {
  48.         for (auto j = 0; j < v.size() - 1; j++) {
  49.             if (v[j] > v[j + 1]) {
  50.                 swap(v, j, j + 1);
  51.             }
  52.         }
  53.     }
  54.  
  55. }
  56.  
  57. template<typename T>
  58. void printContainer(T v) {
  59.     for (auto i = 0; i < v.size(); i++) {
  60.         cout << v[i] << " ";
  61.     }
  62.     cout << endl;
  63. }
  64.  
  65. template <typename T>
  66. void swap(T& container, const int a, const int b) {
  67.     auto temp = container[a];
  68.     container[a] = container[b];
  69.     container[b] = temp;
  70. }
  71.  
  72. template<typename T>
  73. void selectionSort(T &v) {
  74.  
  75.     for (auto i = 0; i < v.size(); i++) {
  76.         auto min = v[i];
  77.         auto index = i;
  78.         for (auto j = i + 1; j < v.size(); j++) {
  79.             if (v[j] < min) {
  80.                 min = v[j];
  81.                 index = j;
  82.             }
  83.         }
  84.         swap(v, i, index);
  85.     }
  86. }
  87.  
  88. template<typename T>
  89. bool isLessEqualSorted(T v) {
  90.     for (auto i = 0; i < v.size() - 1; i++) {
  91.         if (v[i] > v[i + 1]) {
  92.             return false;
  93.         }
  94.     }
  95.     return true;
  96. }
  97. template <typename T>
  98. string isSorted(T v) {
  99.     if (isLessEqualSorted(v)) {
  100.         return " ist sortiert!\n";
  101.     }
  102.     return " ist nicht sortiert!\n";
  103. }
  104.  
  105.  
  106. template <typename T>
  107. T randomStringContainer(const ContainerType container, const int size, const int stringLength) {
  108.     static const char alphanum[] =
  109.         "0123456789"
  110.         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  111.         "abcdefghijklmnopqrstuvwxyz";
  112.     vector<string> vresult;
  113.     array<string, size> aresult;
  114.     for (auto i = 0; i < size;i++) {
  115.         aresult.push_back("");
  116.         vresult.push_back("");
  117.         for (auto j = 0; j < stringLength;j++) {
  118.             aresult[i] += alphanum[rand() % (sizeof(alphanum) - 1)];
  119.             vresult[i] += alphanum[rand() % (sizeof(alphanum) - 1)];
  120.         }
  121.     }
  122.     if(container == VECTOR) {
  123.         return vresult;
  124.     }
  125.     return aresult;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement