Advertisement
fcamuso

Algoritmi lezione 15 - Insertion Sort, parte 1/2

Mar 5th, 2024
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 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 insertion_sort_interi(unsigned long v[], int numero_elementi)
  13. {
  14.   for (int i = 1; i < numero_elementi; i++) {
  15.     int elemento_corrente = v[i];
  16.  
  17.     int j = i - 1;
  18.     while( j>=0 && v[j] > elemento_corrente)
  19.     {
  20.       v[j + 1] = v[j];
  21.       j--;
  22.     }
  23.     v[j + 1] = elemento_corrente;
  24.   }
  25. }
  26.  
  27. const int QUANTI_ELEMENTI = 50000;
  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.  //lo ordino decrescente
  36.  // ordina_vettore_interi_senza_segno(v, QUANTI_ELEMENTI, false);
  37.  
  38.  
  39.   Cronometro(Stato::START);
  40.  
  41.   //lo ordino crescente
  42.   insertion_sort_interi(v, QUANTI_ELEMENTI);
  43.  
  44.   cout << "Tempo impiegato: " << Cronometro(Stato::STOP) << endl;
  45.   if (verifica_ordine_crescente(v, QUANTI_ELEMENTI)) cout <<"IN ORDINE!\n";
  46.  
  47.   //stampa_vettore_interi(v, QUANTI_ELEMENTI);
  48.  
  49.  
  50.     return 0;
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement