Advertisement
fcamuso

Algoritmi lezione 13 - Bubble Sort

Feb 27th, 2024
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 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. void bubble_sort_interi(unsigned long v[], int numero_elementi)
  13. {
  14.  
  15.   for(int i = 0; i < numero_elementi; i++)
  16.   {
  17.     bool fatti_scambi = false;
  18.  
  19.     for(int j = 0; j < numero_elementi - i - 1; j++)
  20.       if (v[j] > v[j + 1])
  21.       { swap(v[j], v[j + 1]);fatti_scambi=true;}
  22.  
  23.     if (!fatti_scambi) break;
  24.   }
  25. }
  26.  
  27. const int QUANTI_ELEMENTI = 100000;
  28. const int LUNGHEZZA = 1000;
  29. unsigned long v[QUANTI_ELEMENTI];
  30.  
  31. int main()
  32. {
  33.   carica_vettore_interi(v, QUANTI_ELEMENTI);
  34.  
  35.   cout << "Primo sort ...\n";
  36.   bubble_sort_interi(v, QUANTI_ELEMENTI);
  37.  
  38.   cout << "Mischio ...\n";
  39.   mischia_vettore_interi(v,QUANTI_ELEMENTI, 1000);
  40.  
  41.   cout << "Riordino ... \n";
  42.   Cronometro(Stato::START);
  43.   bubble_sort_interi(v, QUANTI_ELEMENTI);
  44.  
  45.   if (!verifica_ordine_crescente(v, QUANTI_ELEMENTI)) cout << "NON ordinato! \nh";
  46.  
  47.   cout << "Tempo impiegato: " << Cronometro(Stato::STOP) << endl;
  48.  
  49.  
  50.   //stampa_vettore_interi(v, QUANTI_ELEMENTI);
  51.  
  52.  
  53.     return 0;
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement