Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Sorting algorithm comparison
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64 #include <iostream> #include <algorithm> #include <string> #include <cstring> #include <ctime> #include <ratio> #include <chrono> #include <iomanip> #include <cstdio> #include <cstdlib> using namespace std; template<typename TimeT = std::chrono::milliseconds> struct measure { template<typename F, typename ...Args> static typename TimeT::rep execution(F func, Args&&... args) { auto start = std::chrono::high_resolution_clock::now(); func(std::forward<Args>(args)...); auto duration = std::chrono::duration_cast< TimeT> (std::chrono::high_resolution_clock::now() - start); return duration.count(); } }; string s = "", z, tmp; void quicksort() { sort(s.begin(), s.end()); } void bubblesort() { for (int i = 0; i < z.size(); ++i) { for (int j = i + 1; j < z.size(); ++j) { if (z[j] < z[i]) { char c = z[i]; z[i] = z[j]; z[j] = c; } } } } void linearsort() { int arr[256]; for (int i = 0; i < 256; ++i) arr[i] = 0; char* out = (char*)malloc(tmp.length() * sizeof(char)); for (int i = tmp.length() - 1; i >= 0; --i) arr[(int)s[i]]++; for (int i = 0, l = 0; i < 256; ++i) while (arr[i]--) out[l++] = (char)i; tmp = string(out); free(out); } #define B 1000000000 int main() { while (cin>>tmp) { s += tmp; } s.erase(remove(s.begin(), s.end(), '\n'), s.end()); s.erase(remove(s.begin(), s.end(), '\r'), s.end()); z = s; tmp = s; auto t = measure<std::chrono::nanoseconds>::execution(quicksort); cout<<"quicksort:\t"<<t<<" ns.\t("<<setprecision(4)<<(double)t/B<<" s.)"<<endl; t = measure<std::chrono::nanoseconds>::execution(bubblesort); cout<<"bubblesort:\t"<<t<<" ns.\t("<<setprecision(4)<<(double)t/B<<" s.)"<<endl; t = measure<std::chrono::nanoseconds>::execution(linearsort); cout<<"linearsort:\t"<<t<<" ns.\t("<<setprecision(4)<<(double)t/B<<" s.)"<<endl; // cout<<(s.compare(z) == 0)<<"\t"<<(s.compare(tmp) == 0)<<endl; //cout<<s.length()<<"\t"<<tmp.length()<<endl; //cout<<endl<<s<<endl<<endl<<tmp<<endl; }
run
|
edit
|
history
|
help
0
#26
#22.2
Enum Class Comparisons
Zero
Regex pipe and tabs c++
typeid VC++
Default constructor - none defined
Time Zone Registry
hangman
Affine Key Finder and Decrypter