Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <tuple>
- using namespace std;
- template <typename T>
- void bubble_sort(vector<T> arr, bool ascending) {
- for (size_t i = 0; i < arr.size() - 1; i++)
- for (size_t j = 0; j < arr.size() - i - 1; j++)
- if (!(arr[j] < arr[j+1]))
- swap(arr[j], arr[i]);
- if (ascending)
- reverse(arr.begin(), arr.end());
- }
- template <typename T>
- class Number {
- public:
- T real;
- T imaginary;
- bool operator<(const Number& other) {
- return tie(this->real, this->imaginary) < tie(other.real, other.imaginary);
- }
- };
- template <typename T>
- ostream& operator<<(ostream& os, Number<T> number) {
- os << "{" << number.real << " " << number.imaginary << "}";
- return os;
- }
- template <typename T>
- ostream& operator<< (ostream& os, vector<T> arr) {
- for (auto el : arr)
- os << el << " ";
- return os << endl;
- }
- int main() {
- cout << "Vector of ints: \n";
- vector<int> a = {10, 8, 7, 6, 3, 2};
- cout << a;
- bubble_sort(a, false);
- cout << a;
- cout << "Vector of doubles: \n";
- vector<double> b = {123.21, 21.3435, 23, 32423};
- cout << b;
- bubble_sort(b, true);
- cout << b;
- cout << "Numbers: \n";
- vector<Number<int>> c = {{1, 2}, {3, 4}, {5, 9}};
- cout << c;
- bubble_sort(c, false);
- cout << c;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement