#include <iostream>
#include <vector>
#include <algorithm>
#include <chrono>
using namespace std;
template <class F>
chrono::milliseconds take_time(F&& f)
{
auto const begin = chrono::system_clock::now();
f();
auto const end = chrono::system_clock::now();
return chrono::duration_cast<chrono::milliseconds>( end - begin );
}
int main()
{
vector< vector<int> > arr {
{1,5,3,4},
{0,4,1,0},
{7,2,3,1}
};
auto const time = take_time([&] {
vector<int> tmp;
for (auto& r : arr)
copy( r.begin(), r.end(), back_inserter( tmp ) );
sort( tmp.begin(), tmp.end() );
for (unsigned r=0; r<arr.size(); ++r) {
for (unsigned c=0; c<arr[r].size(); ++c) {
const unsigned i= r * arr[r].size() + c;
arr[r][c] = tmp[i];
}
}
});
/* results */
for (auto& row : arr)
{
for (auto& col : row)
cout << col << ", ";
cout << '\n';
}
cout << time.count() << " ms." << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8Y2hyb25vPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnRlbXBsYXRlIDxjbGFzcyBGPgpjaHJvbm86Om1pbGxpc2Vjb25kcyB0YWtlX3RpbWUoRiYmIGYpCnsKCWF1dG8gY29uc3QgYmVnaW4gPSBjaHJvbm86OnN5c3RlbV9jbG9jazo6bm93KCk7CglmKCk7CglhdXRvIGNvbnN0IGVuZCA9IGNocm9ubzo6c3lzdGVtX2Nsb2NrOjpub3coKTsKCXJldHVybiBjaHJvbm86OmR1cmF0aW9uX2Nhc3Q8Y2hyb25vOjptaWxsaXNlY29uZHM+KCBlbmQgLSBiZWdpbiApOwp9CgppbnQgbWFpbigpCnsKCXZlY3RvcjwgdmVjdG9yPGludD4gPiBhcnIgewoJCXsxLDUsMyw0fSwKCQl7MCw0LDEsMH0sCgkJezcsMiwzLDF9Cgl9OwoJYXV0byBjb25zdCB0aW1lID0gdGFrZV90aW1lKFsmXSB7CgkJdmVjdG9yPGludD4gdG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