Skip to content

Instantly share code, notes, and snippets.

@thomastay
Created May 15, 2018 04:52
Show Gist options
  • Save thomastay/f4da34e0108a1b1e571dca7fbd95bb57 to your computer and use it in GitHub Desktop.
Save thomastay/f4da34e0108a1b1e571dca7fbd95bb57 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
#include <random>
using namespace std;
//Takes in as input a vector, and sorts it
void testA(vector<int> &v, int N){
sort(begin(v), end(v));
for (auto i = v.begin(); i < v.begin() + N; ++i){
cout << *i << " ";
}
cout << "\n";
for (auto i = v.rbegin(); i < v.rbegin() + N; ++i){
cout << *i << " ";
}
cout << endl;
}
void testB(vector<int> &v, int N){
std::nth_element(v.begin(), v.begin() + N, v.end());
std::sort(v.begin(), v.begin() + N);
for (auto i = v.begin(); i < v.begin() + N; ++i){
cout << *i << " ";
}
cout << "\n";
std::nth_element(v.rbegin(), v.rbegin() + N, v.rend(), std::greater<int>());
std::sort(v.rbegin(), v.rbegin() + N, std::greater<int>());
for (auto i = v.rbegin(); i < v.rbegin() + N; ++i){
cout << *i << " ";
}
cout << endl;
}
int main(int argc, char **argv) {
random_device rd;
mt19937 g(rd());
std::ios_base::sync_with_stdio(false);
vector<int> testVec{5,6,7,1,2,3,7,9,8};
char* temp = argv[2];
int size = atoi(temp);
vector<int> testVec2(size);
iota (testVec2.begin(), testVec2.end(), 0);
shuffle(testVec2.begin(), testVec2.end(), g);
//Should return 1,2,3,5,6 and 9,8,7,7,6
if (argv[1][0] == 'a')
testA(testVec2, 100);
else if (argv[1][0] == 'b')
testB(testVec2, 100);
else
cout << "Oops" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment