diff --git a/lab05/01.cpp b/lab05/01.cpp new file mode 100644 index 0000000..2851166 --- /dev/null +++ b/lab05/01.cpp @@ -0,0 +1,159 @@ +#include +#include +#include +#include +using namespace std; + +#define SIZE 5000 + +void benchmark(void(*f)(vector* v), vector* v); +void sort_shell(vector* v); +void sort_heap(vector* v); +void sort_quick_driver(vector* v); + +int main(){ + ios::sync_with_stdio(false); + + cout<<"----- Testing sort algorithms -----\n\n\n"; + cout<<"Usage: e[X]it, [C]reate vector, print [V]ector, [R]eset vector, [S]hell sort, [H]eap sort, [Q]uick sort\n\n"; + + srand(time(NULL)); + + vector v; + vector work; + + while(true){ + char command; + cout<<"> "; + cin>>command; + + switch(command){ + case 'X': + case 'x': + cout<<"Exiting..."<::iterator it = work.begin(); it < work.end(); it++){ + cout<<*it<<" "; + } + cout<<"\n"; + break; + case 'R': + case 'r': + cout<<"Reinitializing working copy...\n"; + work = v; + cout<<"Done\n"; + break; + case 'S': + case 's': + cout<<"# Shell sort\n"; + benchmark(sort_shell, &work); + break; + case 'H': + case 'h': + cout<<"# Heap sort\n"; + benchmark(sort_heap, &work); + break; + case 'Q': + case 'q': + cout<<"# Quick sort\n"; + benchmark(sort_quick_driver, &work); + } + } + + return 0; +} + + + +void benchmark(void(*f)(vector* v), vector* v){ + auto t1 = chrono::high_resolution_clock::now(); + (*f)(v); + auto t2 = chrono::high_resolution_clock::now(); + auto duration = chrono::duration_cast(t2 - t1).count(); + cout<<"Execution time: "<* v){ + for(int gap = v->size()/2; gap>0; gap/=2){ + for(int i = gap; i < v->size(); i++){ + int temp = v->at(i); + + int j; + for(j=i; j >= gap && v->at(j-gap) > temp; j-=gap){ + v->at(j) = v->at(j-gap); + } + + v->at(j) = temp; + } + } +} + +void sort_heap_heapify(vector* v, int n, int i){ + int largest = i; + int left = 2 * i + 1; + int right = 2 * i + 2; + + if(left < n && v->at(left) > v->at(largest)){ + largest = left; + } + + if(right < n && v->at(right) > v->at(largest)){ + largest = right; + } + + if(largest != i){ + swap(v->at(i), v->at(largest)); + + sort_heap_heapify(v, n, largest); + } +} +void sort_heap(vector* v){ + for(int i = v->size()/2 - 1; i>=0; i--){ + sort_heap_heapify(v, v->size(), i); + } + + for(int i = v->size() - 1; i>0; i--){ + swap(v->at(0), v->at(i)); + + sort_heap_heapify(v, i, 0); + } +} + +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/lab05/02.cpp b/lab05/02.cpp new file mode 100644 index 0000000..b78f12a --- /dev/null +++ b/lab05/02.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +using namespace std; + +#define N 20 + +void sort_shell(vector* v){ + for(int gap = v->size()/2; gap>0; gap/=2){ + for(int i = gap; i < v->size(); i++){ + int temp = v->at(i); + + int j; + for(j=i; j >= gap && v->at(j-gap) > temp; j-=gap){ + v->at(j) = v->at(j-gap); + } + + v->at(j) = temp; + } + } +} + +void sort_heap_heapify(vector* v, int n, int i){ + int smallest = i; + int left = 2 * i + 1; + int right = 2 * i + 2; + + if(left < n && v->at(left) < v->at(smallest)){ + smallest = left; + } + + if(right < n && v->at(right) < v->at(smallest)){ + smallest = right; + } + + if(smallest != i){ + swap(v->at(i), v->at(smallest)); + + sort_heap_heapify(v, n, smallest); + } +} +void sort_heap(vector* v){ + for(int i = v->size()/2 - 1; i>=0; i--){ + sort_heap_heapify(v, v->size(), i); + } + + for(int i = v->size() - 1; i>0; i--){ + swap(v->at(0), v->at(i)); + + sort_heap_heapify(v, i, 0); + } +} + +int main(){ + ios::sync_with_stdio(false); + + vector v; + + srand(time(NULL)); + + cout<<"The vector:\n"; + for(int i=0; i par; + vector impar; + for(int i=0; i +#include +#include +using namespace std; + +class Subscriber{ +public: + string name, address, phone; + double invoice; + + void print(){ + cout<<"- Name: "<* v, int low, int high){ + int pivot = v->at(high).invoice; + int i = low - 1; + + for(int j = low; j <= high - 1; j++){ + if(v->at(j).invoice > 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); +} + +int main(){ + ios::sync_with_stdio(false); + + cout<<"Number of subscribers: "; + int n; + cin>>n; + + vector v; + for(int i=0; i>s.invoice; + + v.push_back(s); + } + + sort_quick_driver(&v); + + cout<<"\n\n\nTop 3 invoice values:\n"; + for(int i=0; i<3; i++){ + v[i].print(); + } + + cout.flush(); + + return 0; +}