-
Notifications
You must be signed in to change notification settings - Fork 0
/
comparator.cpp
48 lines (40 loc) · 1.15 KB
/
comparator.cpp
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
#include<bits/stdc++.h>
using namespace std;
bool should_i_swap(int a, int b) {
if(a < b) return true;
return false;
}
bool should_i_swap(pair<int,int> a, pair<int,int> b) {
if (a.first != b.first) {
if (a.first > b.first) return true;
return false;
} else {
if (a.second < b.second) return true;
return false;
}
}
// comparator me whi return karwado, jis order me haame final sorted array chahiye
bool cmp(pair<int,int> a, pair<int,int> b) {
if (a.first != b.first) {
return a.first < b.first;
}
return a.second > b.second;
}
int main() {
// vector<int> a {56, 34, 23, 2, 55, 32, 8};
vector<pair<int,int>> a {{2, 5}, {5, 3}, {5, 5}, {7, 8}, {9, 2}, {9, 6}, {1, 5}};
int n = a.size();
// Custom logic of sort in O(n^2)
// for(int i = 0; i < n; ++i) {
// for(int j = i+1; j < n; ++j) {
// if(should_i_swap(a[i], a[j])) {
// swap(a[i], a[j]);
// }
// }
// }
sort(a.begin(), a.end(), cmp);
for(int i = 0; i < n; ++i) {
cout << a[i].first << " " << a[i].second << endl;
}
return 0;
}