Last active
June 8, 2018 15:04
-
-
Save Leo1003/8f765ab13929534cd4ffa93ac50d3330 to your computer and use it in GitHub Desktop.
random_sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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