-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathvector_argsort.cpp
55 lines (44 loc) · 1.36 KB
/
vector_argsort.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
49
50
51
52
53
54
55
#include <iostream>
#include <vector>
#include <numeric> // std::iota
#include <algorithm> // std::sort
#include <iterator> // std::ostream_iterator
using namespace std;
//note that the argument is passed by reference
//so v will be modified!
template <typename T>
vector<size_t> sort_indexes(const vector<T> &v) {
// initialize original index locations
vector<size_t> idx(v.size());
iota(idx.begin(), idx.end(), 0);
// sort indexes based on comparing values in v
sort(idx.begin(), idx.end(),
[&v](size_t i1, size_t i2) {return v[i1] < v[i2];});
return idx;
}
int main()
{
vector<int> v = {5,2,8,0,1,9};
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "nth smallest element is current at what position?" << endl;
vector<size_t> ixs = sort_indexes(v);
copy(ixs.begin(), ixs.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "The element should be put into ?th position:" << endl;
vector<size_t> putIxs(v.size());
for(int i = 0; i < v.size(); i++){
int ix = ixs[i];
putIxs[ix] = i;
}
copy(putIxs.begin(), putIxs.end(), ostream_iterator<int>(cout, " "));
cout << endl;
return 0;
}
/*
5 2 8 0 1 9
nth smallest element is current at what position?
3 4 1 0 2 5
The element should be put into ?th position:
3 2 4 0 1 5
*/