Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <array>
- #include <cstdlib>
- using namespace std;
- const size_t Size1{ 100 };
- const size_t Size2{ 5 };
- template<typename D>
- void bubbleSort(D& container)
- {
- for (size_t end{ container.size() }; end > 1; --end)
- {
- for (size_t index = 0; index < end - 1; index++)
- {
- if (container[index] > container[index + 1])
- {
- auto temp{ container[index + 1] };
- container[index + 1] = container[index];
- container[index] = temp;
- }
- }
- }
- }
- template<typename C>
- void selectionSort(C& container)
- {
- unsigned int maxIndex{ container.size() - 1 };
- unsigned int placeIndex{ 0 };
- while (placeIndex < maxIndex)
- {
- unsigned int minIndex{ placeIndex };
- for (size_t i = placeIndex ; i <= maxIndex; i++)
- {
- if (container[i] < container[minIndex])
- minIndex = i;
- }
- auto temp{ container[placeIndex] };
- container[placeIndex] = container[minIndex];
- container[minIndex] = temp;
- ++placeIndex;
- }
- }
- template<typename B>
- bool lessEqualSorted(B& container)
- {
- bool sorted{ true };
- for (size_t index = 0; index < container.size() - 1; index++)
- {
- if (container[index] > container[index + 1])
- sorted = false;
- }
- return sorted;
- }
- template<typename A>
- void print(const A& container)
- {
- for (const auto& container : container)
- cout << setw(4) << container << " ";
- cout << endl;
- }
- int main()
- {
- srand(time(nullptr));
- array< unsigned int, Size1> nummberListBB{ 0 };
- array< unsigned int, Size1> nummberListSS{ 0 };
- array< string, Size2> stringListBB{ "Charlie", "Delta", "Echo", "Bravo", "Alfa" };
- array< string, Size2> stringListSS{ "Charlie", "Delta", "Echo", "Bravo", "Alfa" };
- for (size_t index = 0; index < Size1; index++)
- {
- nummberListBB[index] = 1 + rand() % 1000;
- nummberListSS[index] = 1 + rand() % 1000;
- }
- bubbleSort(nummberListBB);
- bubbleSort(stringListBB);
- selectionSort(nummberListSS);
- selectionSort(stringListSS);
- cout << "Bubble Sort: " << endl;
- print(nummberListBB);
- cout << "Sortiert: " << boolalpha << lessEqualSorted(nummberListBB) << endl << endl;
- print(stringListBB);
- cout << "Sortiert: " << boolalpha << lessEqualSorted(stringListBB) << endl;
- cout << " \nSelection Sort: " << endl;
- print(nummberListSS);
- cout << "Sortiert: " << boolalpha << lessEqualSorted(nummberListSS) << endl << endl;
- print(stringListSS);
- cout << "Sortiert: " << boolalpha << lessEqualSorted(stringListSS) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement