Advertisement
bartekltg

ponowne odkrycie binary insertion sort na psf

Dec 20th, 2022
1,199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <random>
  6. #include <chrono>
  7.  
  8.  
  9. using namespace std;
  10.  
  11. class timer{
  12.     chrono::time_point<chrono::high_resolution_clock> a,b;
  13. public:
  14.     void start(){a = chrono::high_resolution_clock::now();}
  15.     void stop() {b = chrono::high_resolution_clock::now();}
  16.     double value(){
  17.         chrono::duration<double> elapsed_seconds = b-a;
  18.         return elapsed_seconds.count();
  19.     }
  20.     void show(){
  21.         cout << value()<<" s"<<endl;
  22.     }
  23. };
  24. mt19937 r;
  25.  
  26.  
  27. string losowe_slowo(){
  28.     uniform_int_distribution<int> gen_dl(4,15);
  29.     uniform_int_distribution<int> gen_c(0,'z'-'a');
  30.     string s="";
  31.     generate_n(back_inserter(s),gen_dl(r), [&](){ return (char)'a'+gen_c(r); });
  32.     return s;
  33. }
  34.  
  35. void generuj (vector<string> &tab, size_t n){
  36.     tab.clear();
  37.     generate_n(back_inserter(tab),n, losowe_slowo);
  38. }
  39.  
  40. void test1(vector<string> tab){
  41.     timer stoper;
  42.  
  43.     stoper.start();
  44.     sort(begin(tab), end(tab));
  45.     stoper.stop();
  46.     cout<<"posortowal qsortem w "<<stoper.value()<<"s\n";
  47.     if (!is_sorted(begin(tab), end(tab)))
  48.         cout<<"SORTOWANIE SPARTOLONO"<<endl;
  49. }
  50.  
  51. void test2(vector<string> tab){
  52.     timer stoper;
  53.  
  54.     vector<string> tab_out;
  55.     tab_out.reserve(tab.size()); //dajemy fory tej metodzie
  56.  
  57.     stoper.start();
  58.  
  59.     for (auto v: tab){
  60.         tab_out.insert( lower_bound(begin(tab_out),end(tab_out),v),v );
  61.     }
  62.     stoper.stop();
  63.     cout<<"posortowal BIStem w "<<stoper.value()<<"s\n";
  64.     if (!is_sorted(begin(tab_out), end(tab_out)))
  65.         cout<<"SORTOWANIE SPARTOLONO"<<endl;
  66. }
  67.  
  68.  
  69. int main(){
  70.     r.seed(1000500);
  71.  
  72.     vector <string> tab;
  73.  
  74.     for (int i=100;i<1000000; i*=10 ){
  75.         generuj(tab,i);
  76.         cout<<"Sortuj "<<i<<" wyrazow"<<endl;
  77.         test1(tab); //tablica przekazywana jako kopia
  78.         test2(tab);
  79.         cout<<endl;
  80.     }
  81.     return 0;
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement