Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <array>
  5. #include <cstdlib>
  6. using namespace std;
  7.  
  8. const size_t Size1{ 100 };
  9. const size_t Size2{ 5 };
  10.  
  11. template<typename D>
  12. void bubbleSort(D& container)
  13. {
  14.     for (size_t end{ container.size() }; end > 1; --end)
  15.     {
  16.         for (size_t index = 0; index < end - 1; index++)
  17.         {
  18.             if (container[index] > container[index + 1])
  19.             {
  20.                 auto temp{ container[index + 1] };
  21.                 container[index + 1] = container[index];
  22.                 container[index] = temp;
  23.             }
  24.         }
  25.     }
  26. }
  27.  
  28. template<typename C>
  29. void selectionSort(C& container)
  30. {
  31.     unsigned int maxIndex{ container.size() - 1 };
  32.     unsigned int placeIndex{ 0 };
  33.     while (placeIndex < maxIndex)
  34.     {
  35.         unsigned int minIndex{ placeIndex };
  36.         for (size_t i = placeIndex ; i <= maxIndex; i++)
  37.         {
  38.             if (container[i] < container[minIndex])
  39.                 minIndex = i;
  40.         }
  41.         auto temp{ container[placeIndex] };
  42.         container[placeIndex] = container[minIndex];
  43.         container[minIndex] = temp;
  44.         ++placeIndex;
  45.     }
  46. }
  47.  
  48. template<typename B>
  49. bool lessEqualSorted(B& container)
  50. {
  51.     bool sorted{ true };
  52.     for (size_t index = 0; index < container.size() - 1; index++)
  53.     {
  54.         if (container[index] > container[index + 1])
  55.             sorted = false;
  56.     }
  57.     return sorted;
  58. }
  59.  
  60. template<typename A>
  61. void print(const A& container)
  62. {
  63.     for (const auto& container : container)
  64.         cout << setw(4) << container << " ";
  65.  
  66.     cout << endl;
  67. }
  68.  
  69.  
  70. int main()
  71. {
  72.     srand(time(nullptr));
  73.  
  74.     array< unsigned int, Size1>  nummberListBB{ 0 };
  75.     array< unsigned int, Size1>  nummberListSS{ 0 };
  76.     array< string, Size2> stringListBB{ "Charlie", "Delta", "Echo", "Bravo", "Alfa" };
  77.     array< string, Size2> stringListSS{ "Charlie", "Delta", "Echo", "Bravo", "Alfa" };
  78.  
  79.     for (size_t index = 0; index < Size1; index++)
  80.     {
  81.         nummberListBB[index] = 1 + rand() % 1000;
  82.         nummberListSS[index] = 1 + rand() % 1000;
  83.     }
  84.  
  85.     bubbleSort(nummberListBB);
  86.     bubbleSort(stringListBB);
  87.  
  88.     selectionSort(nummberListSS);
  89.     selectionSort(stringListSS);
  90.  
  91.  
  92.     cout << "Bubble Sort: " << endl;
  93.     print(nummberListBB);
  94.     cout << "Sortiert: " << boolalpha << lessEqualSorted(nummberListBB) << endl << endl;
  95.     print(stringListBB);
  96.     cout << "Sortiert: " << boolalpha << lessEqualSorted(stringListBB) << endl;
  97.  
  98.  
  99.     cout << " \nSelection Sort: " << endl;
  100.     print(nummberListSS);
  101.     cout << "Sortiert: " << boolalpha << lessEqualSorted(nummberListSS) << endl << endl;
  102.     print(stringListSS);
  103.     cout << "Sortiert: " << boolalpha << lessEqualSorted(stringListSS) << endl;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement