Advertisement
M3IY0U

Untitled

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