From 0cf994e5fdb5db06052ee55b160a3f42f3282751 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1ndly=20Gerg=C5=91?= Date: Sat, 16 May 2020 15:52:34 +0300 Subject: [PATCH] chore: Lab09 --- lab09/02.cpp | 215 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 lab09/02.cpp diff --git a/lab09/02.cpp b/lab09/02.cpp new file mode 100644 index 0000000..6e71c76 --- /dev/null +++ b/lab09/02.cpp @@ -0,0 +1,215 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +string generateWord() { + const string charset = "abcdefghijklmnopqrstuvwxyz"; + int length = rand() % 20 + 1; + string res = ""; + for(int i=0; ival = val; + next = NULL; + } + string val; + ListItem* next; +}; + +class List { +private: + ListItem* first; + int cnt; +public: + List() { + first = NULL; + cnt = 0; + } + ListItem* begin() { + return first; + } + ListItem** beginPtr() { + return &first; + } + void push_back(string val) { + if(first == NULL) { + first = new ListItem(val); + } + else { + ListItem* i = first; + while(i->next != NULL){ + i = i->next; + } + i->next = new ListItem(val); + } + cnt++; + } + void clear() { + for(ListItem* i = first; i;) { + ListItem* cur = i; + i = i->next; + delete cur; + } + first = NULL; + cnt = 0; + } + int size() { + return cnt; + } + + static ListItem* getLast(ListItem* i) { + while(i->next != NULL){ + i = i->next; + } + return i; + } +}; + +void benchmarkSorts(List* v1, vector* v2, int size); +void sort_quick_driver(vector* v); +void sort_quick_driver(List* v); + +int main() { + ios::sync_with_stdio(false); + srand(time(0)); + + List v1; + vector v2; + + benchmarkSorts(&v1, &v2, 10); + benchmarkSorts(&v1, &v2, 1000); + benchmarkSorts(&v1, &v2, 10000); + + return 0; +} + + + +void generateSample(List* v1, vector* v2, int size) { + v1->clear(); + v2->clear(); + cout<<"----- Sample size: "<push_back(w); + v2->push_back(w); + } +} +void benchmarkSorts(List* v1, vector* v2, int size) { + generateSample(v1, v2, size); + + chrono::high_resolution_clock::time_point v1t1 = chrono::high_resolution_clock::now(); + sort_quick_driver(v1); + chrono::high_resolution_clock::time_point v1t2 = chrono::high_resolution_clock::now(); + int v1d = chrono::duration_cast(v1t2 - v1t1).count(); + cout<<"Quick sort time for list: "<(v2t2 - v2t1).count(); + cout<<"Quick sort time for vector: "<* v, int low, int high) { + string 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); +} + +ListItem* sort_quick_partition(ListItem* low, ListItem* high, ListItem** newLow, ListItem** newHigh) { + ListItem* pivot = high; + ListItem* prev = NULL; + ListItem* cur = low; + ListItem* tail = pivot; + + while(cur != pivot) { + if(cur->val < pivot->val) { + if(*newLow == NULL) { + *newLow = cur; + } + + prev = cur; + cur = cur->next; + } + else { + if(prev) { + prev->next = cur->next; + } + ListItem* tmp = cur->next; + cur->next = NULL; + tail->next = cur; + tail = cur; + cur = tmp; + } + } + + if(*newLow == NULL) { + *newLow = pivot; + } + + *newHigh = tail; + + return pivot; +} +ListItem* sort_quick(ListItem* low, ListItem* high) { + if(!low || low == high) { + return low; + } + + ListItem* newLow = NULL; + ListItem* newHigh = NULL; + + ListItem* pivot = sort_quick_partition(low, high, &newLow, &newHigh); + + if(newLow != pivot) { + ListItem* tmp = newLow; + while(tmp->next != pivot) { + tmp = tmp->next; + } + tmp->next = NULL; + + newLow = sort_quick(newLow, tmp); + tmp = List::getLast(newLow); + tmp->next = pivot; + } + + pivot->next = sort_quick(pivot->next, newHigh); + + return newLow; +} +void sort_quick_driver(List* v) { + *v->beginPtr() = sort_quick(v->begin(), List::getLast(v->begin())); +}