Skip to content

Instantly share code, notes, and snippets.

@Leo1003
Last active June 8, 2018 15:04
Show Gist options
  • Save Leo1003/8f765ab13929534cd4ffa93ac50d3330 to your computer and use it in GitHub Desktop.
Save Leo1003/8f765ab13929534cd4ffa93ac50d3330 to your computer and use it in GitHub Desktop.
random_sorting
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <chrono>
#include <random>
#include <vector>
#include <utility>
using namespace std;
vector<pair<int, int>> arr;
#define NDEBUG
bool comp(pair<int, int> a, pair<int, int> b)
{
return a.second < b.second;
}
void gen_random()
{
unsigned int seed = chrono::system_clock::now().time_since_epoch().count();
mt19937 gen(seed);
uniform_int_distribution<int> dis;
for (int i = 0; i < arr.size(); i++) {
arr[i].second = dis(gen);
}
}
void init_arr(int n)
{
arr.clear();
for (int i = 1; i <= n; i++) {
arr.push_back(make_pair(i, 0));
}
}
int main()
{
while(1) {
int n;
cout << "Enter the number of the array: ";
if(!(cin >> n)) {
break;
}
init_arr(n);
gen_random();
sort(arr.begin(), arr.end(), comp);
#ifdef NDEBUG
for (int i = 0; i < arr.size(); i++) {
cout << arr[i].first << endl;
}
#else
for (int i = 0; i < arr.size(); i++) {
cout << setw(4) << arr[i].first << ":" << arr[i].second << endl;
}
#endif // NDEBUG
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment