Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Sort.hpp
- template <class T>
- void swap(T& v1, T& v2) {
- int tmp = v1;
- v1 = v2;
- v2 = tmp;
- }
- //template <class T>
- //using TLess = bool (const T& value1, const T& value2);
- template <class T, class TLess>
- void BubbleSort(T* arr, int size, TLess less) {
- for (int i = 0; i < size; ++i) {
- for (int j = 1; j < size - i; ++j) {
- if (!less(arr[j - 1], arr[j + i])) {
- swap(arr[j - 1], arr[j]);
- }
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Array
- /*template <class T>
- class Array {
- public:
- explicit Array(int size) { body = new T[size]; }
- Array(const Array<T>&);
- };
- */
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //
- bool less (const int& value1, const int& value2) {
- return value1 > value2;
- }
- struct Point
- {
- int X;
- int Y;
- };
- bool points_less(const Point& p1, const Point& p2) {
- return p1.X != p2.X ? p1.X < p2.X : p1.Y < p2.Y;
- }
- int dist(const Point& p1, const Point& p2) {
- const int dx = p1.X - p2.X;
- const int dy = p1.Y - p2.Y;
- return dx * dx + dy * dy;
- }
- class YLess {
- public:
- YLess() = default;
- YLess(Point _p0) : p0(_p0) {};
- bool operator()(const Point& p1, const Point& p2) {
- return dist(p1, p0) < dist(p2, p0);
- }
- private:
- Point p0 = { 0, 0 };
- };
- int main() {
- int arr[] = { 21, 7, 11, 3, 17, 6, 8, 13 };
- BubbleSort(arr, 8, less);
- Point* points = new Point[4];
- points[0] = { 1, 2 };
- points[1] = {2, 3};
- points[2] = {0, 0};
- points[3] = {1, 1};
- BubbleSort(points, 4, YLess({ 2, 2 }));
- for (int i = 0; i < 4; ++i) {
- std::cout << "(" << points[i].X << ", " << points[i].Y << ") ";
- }
- delete[] points;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement