Advertisement
fcamuso

Algoritmi lezione 16 - Shell Sort

Mar 10th, 2024
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 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. //i due array contengono gli stessi elementi?
  13. //ATTENZIONE: modifica il secondo!
  14. bool stessi_elementi(unsigned long v1[], unsigned long v2[], int QUANTI_ELEMENTI)
  15. {
  16.  
  17.   int rimasti = QUANTI_ELEMENTI;
  18.   for (int i=0; i<QUANTI_ELEMENTI; i++)
  19.   {
  20.       bool trovato = false;
  21.  
  22.       for (int j=0; j<rimasti; j++)
  23.  
  24.       //trovato una corripondenza la sposto
  25.       //a fine array per non riconsiderarla
  26.       //al passo successivo gestendo
  27.       //le occorrenze multiple di un certo valore
  28.       if (v1[i] == v2[j]) {
  29.         trovato=true;
  30.         swap(v2[j], v2[rimasti-1]);
  31.         rimasti --;
  32.         break;
  33.       }
  34.  
  35.       if (!trovato) return false;
  36.   }
  37.   return true;
  38. }
  39.  
  40. void insertion_sort_interi(unsigned long v[], int numero_elementi)
  41. {
  42.   //porta il minimo in prima posizione a fare da sentinella
  43.   int pos_min = 0;
  44.   for (int i=1; i<numero_elementi; i++)
  45.     if (v[i] < v[pos_min]) pos_min = i;
  46.  
  47.   swap(v[0], v[pos_min]);
  48.  
  49.   for (int i = 1; i < numero_elementi; i++) {
  50.  
  51.     int elemento_corrente = v[i];
  52.  
  53.     int j = i - 1;
  54.     while( v[j] > elemento_corrente)
  55.     {
  56.       v[j + 1] = v[j];
  57.       j--;
  58.     }
  59.     v[j + 1] = elemento_corrente;
  60.   }
  61. }
  62.  
  63. void shell_sort_interi(unsigned long v[], int numero_elementi)
  64. {
  65.   int distanza = numero_elementi/2; //'gap'
  66.  
  67.   while (distanza>0)
  68.   {
  69.     for (int i = distanza; i < numero_elementi; i++) {
  70.  
  71.       int elemento_corrente = v[i];
  72.  
  73.       int j = i;
  74.       while( j>=distanza && v[j - distanza] > elemento_corrente)
  75.       {
  76.         v[j] = v[j - distanza];
  77.         j -= distanza;
  78.       }
  79.       v[j] = elemento_corrente;
  80.     }
  81.  
  82.     distanza = distanza / 2;
  83.   }
  84. }
  85.  
  86.  
  87.  
  88.  
  89. const int QUANTI_ELEMENTI = 100000;
  90. const int LUNGHEZZA = 1000;
  91. unsigned long v[QUANTI_ELEMENTI];
  92. unsigned long vcopia[QUANTI_ELEMENTI];
  93.  
  94.  
  95. int main()
  96. {
  97.   carica_vettore_interi(v, QUANTI_ELEMENTI);
  98.  
  99.   //stampa_vettore_interi(v, QUANTI_ELEMENTI);
  100.   //cout << string(40, '*') << endl;
  101.  
  102.   //duplico vettore per controlli finali
  103.   for (int i=0; i<QUANTI_ELEMENTI; i++) vcopia[i] = v[i];
  104.  
  105.   Cronometro(Stato::START);
  106.  
  107.   //lo ordino crescente
  108.   shell_sort_interi(v, QUANTI_ELEMENTI);
  109.  
  110.   cout << "Tempo impiegato: " << Cronometro(Stato::STOP) << endl;
  111.   if (verifica_ordine_crescente(v, QUANTI_ELEMENTI)) cout <<"IN ORDINE!\n";
  112.  
  113.   //sono gli stessi elementi?
  114.   if (!stessi_elementi(v, vcopia, QUANTI_ELEMENTI))
  115.     cout << "Array difformi!!\n";
  116.   else
  117.     cout << "L'array ordinato contiene gli stessi elementi di quello iniziale\n";
  118.  
  119.   //stampa_vettore_interi(v, QUANTI_ELEMENTI);
  120.  
  121.  
  122.     return 0;
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement