Advertisement
fcamuso

Algoritmi lezione 6

Feb 3rd, 2024 (edited)
996
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. utility.h
  2. *********************************************************************************
  3. string genera_stringa_casuale(int lunghezza) {
  4.     static const string alfabeto =
  5.         "0123456789"
  6.         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  7.         "abcdefghijklmnopqrstuvwxyz";
  8.  
  9.     string risultato="";
  10.  
  11.     for (int i = 0; i < lunghezza; ++i)
  12.         risultato += alfabeto[ rand()% alfabeto.length() ];
  13.  
  14.  
  15.     return risultato;
  16. }
  17.  
  18. void carica_vettore_interi(int v[], int dimensione)
  19. {
  20.   for (int i=0; i<dimensione; i++)
  21.     v[i] = rand(); //RAND_MAX
  22. }
  23.  
  24. void carica_vettore_stringhe(string v[], int dimensione, int lunghezza=100)
  25. {
  26.   for (int i=0; i<dimensione; i++)
  27.     v[i] = genera_stringa_casuale(lunghezza);
  28. }
  29.  
  30. enum Stato {START, STOP};
  31. auto Cronometro(Stato stato = Stato::START)
  32. {
  33.   static std::chrono::time_point<std::chrono::system_clock> inizio;
  34.   static std::chrono::time_point<std::chrono::system_clock> fine;
  35.  
  36.   if (stato == Stato::START)
  37.   {
  38.     inizio = chrono::high_resolution_clock::now();
  39.     fine = inizio;
  40.   }
  41.   else
  42.     fine = chrono::high_resolution_clock::now();
  43.  
  44.  
  45.   return chrono::duration_cast<std::chrono::milliseconds>(fine - inizio).count();
  46. }
  47.  
  48. sorgente principale
  49. **********************************************************************************
  50. #include <iostream>
  51. #include <chrono>
  52. #include <ctime>
  53.  
  54. using namespace std;
  55.  
  56. #include "../utility_vettori.h"
  57.  
  58. string cerca_stringa_max(string v[], int dimensione)
  59. {
  60.   string stringa_max = v[0];
  61.   for (int i=1; i<dimensione; i++)
  62.     if (v[i]>stringa_max) stringa_max= v[i];
  63.  
  64.   return stringa_max;
  65. }
  66.  
  67. int cerca_pos_stringa_max(string v[], int dimensione)
  68. {
  69.   int pos_stringa_max = 0;
  70.   for (int i=1; i<dimensione; i++)
  71.     if (v[i]>v[pos_stringa_max]) pos_stringa_max = i;
  72.  
  73.   return pos_stringa_max;
  74. }
  75.  
  76. const int QUANTI_ELEMENTI = 30;
  77. string v[QUANTI_ELEMENTI];
  78.  
  79. //complessità O(n)
  80. int main()
  81. {
  82.   carica_vettore_stringhe(v, QUANTI_ELEMENTI - 1, 1000000);
  83.  
  84.   v[QUANTI_ELEMENTI-1] = string(1000000, 'z');
  85.  
  86.   //ordino dal più piccolo al più grande
  87.   for (int i=0; i<QUANTI_ELEMENTI-1; i++)
  88.     for (int j=i+1; j<QUANTI_ELEMENTI; j++)
  89.       if (v[j]>v[i]) swap(v[i], v[j]);
  90.  
  91.   int numero_run = 1;
  92.   int ripetizioni_per_run = 10000;
  93.  
  94.  
  95. // RICERCA CON RESTITUZIONE DELL'ELEMENTO MASSIMO
  96.   Cronometro(Stato::START);
  97.   for(int conta_run =0; conta_run<numero_run; conta_run++)
  98.     for (int conta=0; conta<ripetizioni_per_run; conta++)
  99.       cerca_stringa_max(v, QUANTI_ELEMENTI);
  100.  
  101.   cout << "Tempo impiegato (ELEMENTO): " << Cronometro(Stato::STOP) << endl;
  102.  
  103.  
  104.  
  105. //RICERCA CON RESTITUZIONE DELLA POSIZIONE DELL'ELEMENTO MASSIMO
  106.   Cronometro(Stato::START);
  107.   for(int conta_run =0; conta_run<numero_run; conta_run++)
  108.     for (int conta=0; conta<ripetizioni_per_run; conta++)
  109.       cerca_pos_stringa_max(v, QUANTI_ELEMENTI);
  110.  
  111.   cout << "Tempo impiegato (POSIZIONE): " << Cronometro(Stato::STOP) << endl;;
  112.  
  113.   return 0;
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement