반응형
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | #include <iostream> #include <cstdio> #include <vector> using namespace std; template <typename T> void _swap(T &a, T &b) { T t = a; a = b; b = t; } template <typename T, typename C> void _qsort(T start, T end, C comp) { if (start == end) return; _swap(*start, *((end - start) / 2 + start)); T pivot = start; T left = start + 1; T right = end - 1; while (left <= right) { while (left <= right && !comp(*pivot, *left)) left++; while (left <= right && !comp(*right, *pivot)) right--; if (left <= right) _swap(*left, *right); } _swap(*pivot, *right); _qsort(start, right, comp); _qsort(right + 1, end, comp); } template <typename T1, typename T2> struct _pair { T1 first; T2 second; bool operator <(const _pair<T1, T2> &a) { if (first == a.first) return second < a.second; return first < a.first; } }; bool comp(const _pair<int, int> &a, const _pair<int, int> &b) { if (a.second == b.second) return a.first < b.first; return a.second < b.second; } _pair<int, int> arr[6]; vector<_pair<int, int> > vc; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int x, y; scanf("%d %d", &x, &y); arr[i] = { x,y }; vc.push_back({ x,y }); } _qsort(arr, arr + n, comp); _qsort(vc.begin(), vc.end(), comp); for (auto i : arr) printf("%d %d\n", i.first, i.second); printf("\n\n"); for (auto i : vc) printf("%d %d\n", i.first, i.second); return 0; } |
출처 :: https://github.com/baactree/Algorithm/blob/master/Stl/sort.cpp
반응형
'Applied > 알고리즘' 카테고리의 다른 글
과반수 찾기 알고리즘 (4) | 2018.04.08 |
---|---|
단절점(Articulation Point), 단절선(Articulation Bridge) (1) | 2018.02.21 |
해시 테이블(Hash Table) (0) | 2018.01.31 |
선형 합동 생성기(Linear Congruential Generator, LCG) (0) | 2018.01.31 |
하노이 탑 K번째 찾기 (2) | 2018.01.26 |