Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <array>
- #include <string>
- using namespace std;
- template <typename T>
- void printContainer(T);
- template <typename T>
- void bubbleSort(T&);
- template <typename T>
- void swap(T&, int, int);
- template <typename T>
- void selectionSort(T&);
- template <typename T>
- bool isLessEqualSorted(T);
- template <typename T>
- string isSorted(T);
- template <typename T>
- T randomStringContainer(const string, const int, const int);
- #define NAME_OF(v) #v
- int main() {
- vector <int> vector1 = randomStringContainer("vector",10,4);
- vector <int> vector2 = { 7,6,5,4,3,2,1,0 };
- printContainer(vector1);
- bubbleSort(vector1);
- printContainer(vector1);
- cout << NAME_OF(vector1) << isSorted(vector1);
- cout << endl;
- printContainer(vector2);
- selectionSort(vector2);
- printContainer(vector2);
- cout << NAME_OF(vector2) << isSorted(vector2);
- }
- template <typename T>
- void bubbleSort(T& v) {
- for (auto i = 0; i < v.size(); i++) {
- for (auto j = 0; j < v.size() - 1; j++) {
- if (v[j] > v[j + 1]) {
- swap(v, j, j + 1);
- }
- }
- }
- }
- template<typename T>
- void printContainer(T v) {
- for (auto i = 0; i < v.size(); i++) {
- cout << v[i] << " ";
- }
- cout << endl;
- }
- template <typename T>
- void swap(T& container, const int a, const int b) {
- auto temp = container[a];
- container[a] = container[b];
- container[b] = temp;
- }
- template<typename T>
- void selectionSort(T &v) {
- for (auto i = 0; i < v.size(); i++) {
- auto min = v[i];
- auto index = i;
- for (auto j = i + 1; j < v.size(); j++) {
- if (v[j] < min) {
- min = v[j];
- index = j;
- }
- }
- swap(v, i, index);
- }
- }
- template<typename T>
- bool isLessEqualSorted(T v) {
- for (auto i = 0; i < v.size() - 1; i++) {
- if (v[i] > v[i + 1]) {
- return false;
- }
- }
- return true;
- }
- template <typename T>
- string isSorted(T v) {
- if (isLessEqualSorted(v)) {
- return " ist sortiert!\n";
- }
- return " ist nicht sortiert!\n";
- }
- template <typename T>
- T randomStringContainer(const string container, const int size, const int stringLength) {
- static const char alphanum[] =
- "0123456789"
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- "abcdefghijklmnopqrstuvwxyz";
- vector<string> vresult;
- array<string, size> aresult;
- for (auto i = 0; i < size;i++) {
- aresult.push_back("");
- vresult.push_back("");
- for (auto j = 0; j < stringLength;j++) {
- aresult[i] += alphanum[rand() % (sizeof(alphanum) - 1)];
- vresult[i] += alphanum[rand() % (sizeof(alphanum) - 1)];
- }
- }
- if(container =="vector") {
- return vresult;
- }
- return aresult;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement