diff --git a/test1/01.cpp b/test1/01.cpp new file mode 100644 index 0000000..c5c11f0 --- /dev/null +++ b/test1/01.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +int generatorf() { + return rand() % 10000 + 1; +} + +void benchmark(void(*f)(vector* v), vector* v); + +void sort_bubble(vector* v); +void sort_quick_driver(vector* v); + +void sort_quick(vector* v, int low, int high); +int sort_quick_partition(vector* v, int low, int high); + +int main() { + ios::sync_with_stdio(false); + + srand(time(0)); + + vector v(1000); + generate(v.begin(), v.end(), generatorf); + + cout<<"The initial vector:\n"; + for(vector::iterator it = v.begin(); it < v.end(); it++) { + cout<<*it<<" "; + } + cout<<"\n\n\n"; + + vector work; + + // quick sort + work = v; + cout<<"--- Quick sort\n"; + benchmark(sort_quick_driver, &work); + + // bubble sort + work = v; + cout<<"--- Bubble sort\n"; + benchmark(sort_bubble, &work); + + cout.flush(); + + return 0; +} + + + +void benchmark(void(*f)(vector* v), vector* v) { + chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now(); + (*f)(v); + chrono::high_resolution_clock::time_point t2 = chrono::high_resolution_clock::now(); + int duration = chrono::duration_cast(t2-t1).count(); + cout<<"Execution time: "<* v) { + bool done = false; + while(!done) { + done = true; + for(vector::iterator it = v->begin(); it < v->end() - 1; it++){ + if(*it > *(it+1)){ + done = false; + int tmp = *it; + *it = *(it+1); + *(it+1) = tmp; + } + } + } +} + +int sort_quick_partition(vector* v, int low, int high) { + int pivot = v->at(high); + int i = low - 1; + + for(int j = low; j <= high - 1; j++) { + if(v->at(j) < pivot) { + i++; + swap(v->at(i), v->at(j)); + } + } + swap(v->at(i+1), v->at(high)); + + return i + 1; +} + +void sort_quick(vector* v, int low, int high) { + if(low < high) { + int parti = sort_quick_partition(v, low, high); + + sort_quick(v, low, parti - 1); + sort_quick(v, parti + 1, high); + } +} + +void sort_quick_driver(vector* v) { + sort_quick(v, 0, v->size() - 1); +} diff --git a/test1/02.cpp b/test1/02.cpp new file mode 100644 index 0000000..e736010 --- /dev/null +++ b/test1/02.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +using namespace std; + +void generatePermutations(vector* chset, int length, string cur = "") { + if(cur.size() >= length) { + cout<::iterator it = chset->begin(); it < chset->end(); it++) { + cur.push_back(*it); + generatePermutations(chset, length, cur); + cur.pop_back(); + } +} + +int main() { + ios::sync_with_stdio(false); + + int n; + cout<<"Char set size: "; + cin>>n; + + vector chset; + for(int i=0; i>c; + chset.push_back(c); + } + + int k; + cout<<"Generated word length: "; + cin>>k; + + cout<<"Generated combinations:\n"; + generatePermutations(&chset, k); + + cout.flush(); + + return 0; +}