#include #include #include #include using namespace std; #define SIZE 300 void benchmark(void(*f)(vector* v), vector* v); void sort_insert(vector* v); void sort_bininsert(vector* v); void sort_select(vector* v); void sort_bubble(vector* v); void sort_shake(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, [I]nsertion sort, [B]inary insertion sort, [S]elect sort, bubble sor[T], s[H]ake 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 'I': case 'i': cout<<"# Insertion sort\n"; benchmark(sort_insert, &work); break; case 'B': case 'b': cout<<"# Binary insertion sort\n"; benchmark(sort_bininsert, &work); break; case 'S': case 's': cout<<"# Select sort\n"; benchmark(sort_select, &work); break; case 'T': case 't': cout<<"# Bubble sort\n"; benchmark(sort_bubble, &work); break; case 'H': case 'h': cout<<"# Shake sort\n"; benchmark(sort_shake, &work); break; } } 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 i=2; isize(); i++){ int x = v->at(i); int j = i-1; while(j>=0 && v->at(j)>x){ v->at(j+1) = v->at(j); j--; } v->at(j+1) = x; } } void sort_bininsert(vector* v){ for(int i=1; isize(); i++){ int x = v->at(i); int start = 0; int dest = i-1; while(start <= dest){ int middle = (start + dest) / 2; if(v->at(middle) > x){ dest = middle-1; } else{ start = middle+1; } for(int j=i-1; j>=start; j--){ v->at(j+1) = v->at(j); } v->at(start) = x; } } } void sort_select(vector* v){ for(int i=0; isize()-1; i++){ int minpos = i; int minval = v->at(i); for(int j=i; jsize(); j++){ if(v->at(j) < minval){ minval = v->at(j); minpos = j; } } v->at(minpos) = v->at(i); v->at(i) = minval; } } void sort_bubble(vector* v){ bool done = false; while(!done){ done = true; for(vector::iterator it = v->begin(); it < v->end()-1; it++){ if(*it > *(it+1)){ int tmp = *it; *it = *(it+1); *(it+1) = tmp; done = false; } } } } void sort_shake(vector* v){ int left = 2; int right = v->size() - 1; int k = v->size() - 1; do{ for(int j=right; j>=left; j--){ if(v->at(j-1) > v->at(j)){ int tmp = v->at(j-1); v->at(j-1) = v->at(j); v->at(j) = tmp; k = j; } } left = k+1; for(int j=1; j<=right; j++){ if(v->at(j-1) > v->at(j)){ int tmp = v->at(j-1); v->at(j-1) = v->at(j); v->at(j) = tmp; k = j; } } right = k-1; }while(left <= right); }