Advertisement
fcamuso

Algoritmi lezione 17 - sort indiretto, parte 1/2

Mar 13th, 2024
974
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. #include <ctime>
  4. #include <random>
  5.  
  6. using namespace std;
  7. #include "../../min_max/utility_vettori.h"
  8.  
  9.  
  10. using namespace std;
  11.  
  12. struct Dati {
  13.   unsigned long chiave_ordinamento;
  14.  
  15.   double v[1000];
  16.  
  17. };
  18.  
  19. void bubble_sort_stringhe_indiretto(string v[], int indice[], int numero_elementi)
  20. {
  21.  
  22.   for(int i = 0; i < numero_elementi; i++)
  23.   {
  24.     bool fatti_scambi = false;
  25.  
  26.     for(int j = 0; j < numero_elementi - i - 1; j++)
  27.       if (v[indice[j]] > v[indice[j + 1]])
  28.       { swap(indice[j], indice[j + 1]);fatti_scambi=true;}
  29.  
  30.     if (!fatti_scambi) break;
  31.   }
  32. }
  33.  
  34. void bubble_sort_stringhe(string v[], int numero_elementi)
  35. {
  36.  
  37.   for(int i = 0; i < numero_elementi; i++)
  38.   {
  39.     bool fatti_scambi = false;
  40.  
  41.     for(int j = 0; j < numero_elementi - i - 1; j++)
  42.       if (v[j] > v[j + 1])
  43.       { swap(v[j], v[j + 1]);fatti_scambi=true;}
  44.  
  45.     if (!fatti_scambi) break;
  46.   }
  47. }
  48.  
  49. void stampa_vettore_stringhe_indiretto(string v[], int indice[], int QUANTI_ELEMENTI)
  50. {
  51.   for (int i=0; i<QUANTI_ELEMENTI; i++)
  52.       cout << v[indice[i]] << endl;
  53. }
  54.  
  55. void carica_vettore_Dati(Dati v[], long dimensione)
  56. {
  57.    mt19937 twister{time(0)};
  58.  
  59.   for (long i=0; i<dimensione; i++)
  60.     v[i].chiave_ordinamento = twister();
  61. }
  62.  
  63. void bubble_sort_Dati(Dati v[], int numero_elementi)
  64. {
  65.  
  66.   for(int i = 0; i < numero_elementi; i++)
  67.   {
  68.     bool fatti_scambi = false;
  69.  
  70.     for(int j = 0; j < numero_elementi - i - 1; j++)
  71.       if (v[j].chiave_ordinamento > v[j + 1].chiave_ordinamento)
  72.       { swap(v[j], v[j + 1]); fatti_scambi=true;}
  73.  
  74.     if (!fatti_scambi) break;
  75.   }
  76. }
  77.  
  78. void bubble_sort_Dati_indiretto(Dati v[], int indice[], int numero_elementi)
  79. {
  80.  
  81.   for(int i = 0; i < numero_elementi; i++)
  82.   {
  83.     bool fatti_scambi = false;
  84.  
  85.     for(int j = 0; j < numero_elementi - i - 1; j++)
  86.       if (v[indice[j]].chiave_ordinamento > v[indice[j + 1]].chiave_ordinamento)
  87.       { swap(indice[j], indice[j + 1]); fatti_scambi=true;}
  88.  
  89.     if (!fatti_scambi) break;
  90.   }
  91. }
  92.  
  93. const int QUANTI_ELEMENTI = 10000;
  94. const int LUNGHEZZA = 10000;
  95.  
  96. string v[QUANTI_ELEMENTI];
  97. int indice[QUANTI_ELEMENTI];
  98.  
  99.  
  100. Dati vDati[QUANTI_ELEMENTI];
  101.  
  102.  
  103. int main()
  104. {
  105.   cout << sizeof(string) << endl;
  106.  
  107.   cout << "Genero vettore ...\n";
  108.   //carica_vettore_stringhe(v, QUANTI_ELEMENTI, LUNGHEZZA);
  109.   carica_vettore_Dati(vDati, QUANTI_ELEMENTI);
  110.  
  111.   //prepara indice
  112.   for (int i=0; i<QUANTI_ELEMENTI; i++) indice[i] = i;
  113.  
  114.   cout << "Parto con il sort ...\n";
  115.  
  116.   Cronometro(Stato::START);
  117.   bubble_sort_Dati(vDati, QUANTI_ELEMENTI);
  118.  
  119.   cout << "Tempo impiegato: " << Cronometro(Stato::STOP) << endl;
  120.  
  121.   //stampa_vettore_stringhe(v, QUANTI_ELEMENTI);
  122.   //stampa_vettore_stringhe_indiretto(v, indice, QUANTI_ELEMENTI);
  123.  
  124.  
  125.     return 0;
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement