Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <tuple>
  5.  
  6. using namespace std;
  7.  
  8. template <typename T>
  9. void bubble_sort(vector<T> arr, bool ascending) {
  10.     for (size_t i = 0; i < arr.size() - 1; i++)
  11.         for (size_t j = 0; j < arr.size() - i - 1; j++)
  12.             if (!(arr[j] < arr[j+1]))
  13.                 swap(arr[j], arr[i]);
  14.  
  15.     if (ascending)
  16.         reverse(arr.begin(), arr.end());
  17. }
  18.  
  19. template <typename T>
  20. class Number {
  21. public:
  22.     T real;
  23.     T imaginary;
  24.  
  25.     bool operator<(const Number& other) {
  26.         return tie(this->real, this->imaginary) < tie(other.real, other.imaginary);
  27.     }
  28. };
  29.  
  30. template <typename T>
  31. ostream& operator<<(ostream& os, Number<T> number) {
  32.     os << "{" << number.real << " " << number.imaginary << "}";
  33.     return os;
  34. }
  35.  
  36. template <typename T>
  37. ostream& operator<< (ostream& os, vector<T> arr) {
  38.     for (auto el : arr)
  39.         os << el << " ";
  40.     return os << endl;
  41. }
  42.  
  43. int main() {
  44.  
  45.     cout << "Vector of ints: \n";
  46.     vector<int> a = {10, 8, 7, 6, 3, 2};
  47.     cout << a;
  48.     bubble_sort(a, false);
  49.     cout << a;
  50.  
  51.     cout << "Vector of doubles: \n";
  52.     vector<double> b = {123.21, 21.3435, 23, 32423};
  53.     cout << b;
  54.     bubble_sort(b, true);
  55.     cout << b;
  56.  
  57.     cout << "Numbers: \n";
  58.     vector<Number<int>> c = {{1, 2}, {3, 4}, {5, 9}};
  59.     cout << c;
  60.     bubble_sort(c, false);
  61.     cout << c;
  62.  
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement