-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
47 lines (40 loc) · 1.11 KB
/
main.cpp
File metadata and controls
47 lines (40 loc) · 1.11 KB
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
#include <iostream>
#include <vector>
#include <algorithm>
bool isSorted(const std::vector<int>& arr) {
for (size_t i = 1; i < arr.size(); i++)
if (arr[i] < arr[i-1]) return false;
return true;
}
bool isPermutation(const std::vector<int>& candidate, const std::vector<int>& original) {
std::vector<int> a = candidate, b = original;
std::sort(a.begin(), a.end());
std::sort(b.begin(), b.end());
return a == b;
}
void metaSort(std::vector<int>& arr) {
int n = arr.size();
std::vector<int> indices(n, 0);
while (true) {
std::vector<int> candidate(n);
for (int i = 0; i < n; i++)
candidate[i] = arr[indices[i]];
if (isSorted(candidate) && isPermutation(candidate, arr)) {
arr = candidate;
return;
}
int pos = n - 1;
while (pos >= 0 && ++indices[pos] == n) {
indices[pos] = 0;
pos--;
}
if (pos < 0) break;
}
}
int main() {
std::vector<int> arr = {9, 234, 5};
metaSort(arr);
for (int x : arr) std::cout << x << " ";
std::cin.get();
return 0;
}