Advertisement
Guest User

CODICE ESERCIZIO 3 ESERCITAZIONE 1 MURGIA ANDREA

a guest
Apr 1st, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4.  
  5. typedef enum{ORDINATO, QUASI_ORDINATO, INV_ORDINATO, CASUALE} inputType;
  6.  
  7. #define ORDINATO 0
  8. #define QUASI_ORDINATO 1
  9. #define INV_ORDINATO 2
  10. #define CASUALE 3
  11.  
  12. int ricercaBanale (int *a, int numric, int dim);
  13. int* genera_array(int dimensione, inputType tipo_schema);
  14. void selectionSort(int *a, int n);
  15. void swap(int *array, int a , int b);
  16. void stampaRisRicBan(int *a, int elem, int n);
  17.  
  18. int main() {
  19.     srand(time(NULL));
  20.     int elem, i;
  21.     int *a;
  22.  
  23.     printf("\nChe elemento vuoi cercare?");
  24.     scanf("%d",&elem);
  25.  
  26.     printf("\nRICERCA BANALE:\n");
  27.     a = genera_array(100000, CASUALE);
  28.     selectionSort(a,100000);
  29.     stampaRisRicBan(a, elem, 100000);
  30.  
  31.     return 0;
  32. }
  33.  
  34. int ricercaBanale (int *a, int numric, int dim){
  35.     int pos = 0;
  36.  
  37.     do{
  38.         if (numric == a[pos])
  39.             return pos;
  40.         else
  41.             pos++;
  42.     }while (pos < dim && a[pos] <= numric);
  43.  
  44.     return -1;
  45. }
  46.  
  47. int* genera_array(int dimensione, inputType tipo_schema){
  48.     int *array;
  49.     int i, tmp;
  50.  
  51.     array = malloc(dimensione * sizeof(int));
  52.  
  53.     if (array == NULL)
  54.         exit(-1);
  55.  
  56.     switch(tipo_schema){
  57.  
  58.         case ORDINATO:
  59.             for(i=0;i<dimensione;i++)
  60.                 array[i] = i;
  61.             break;
  62.  
  63.         case QUASI_ORDINATO:
  64.             for(i=0;i<dimensione/2;i++)
  65.                 array[i] = i;
  66.  
  67.             for(i=dimensione/2;i<dimensione;i++)
  68.                 array[i] = rand();
  69.  
  70.             break;
  71.  
  72.         case INV_ORDINATO:
  73.             tmp = dimensione-1;
  74.             for(i=0;i<dimensione;i++) {
  75.                 array[i] = tmp;
  76.                 tmp--;
  77.             }
  78.             break;
  79.  
  80.         case CASUALE:
  81.             for(i=0;i<dimensione;i++)
  82.                 array[i] = rand();
  83.  
  84.             break;
  85.     }
  86.  
  87.     return array;
  88. }
  89.  
  90. void selectionSort(int *a, int n){
  91.     int i, j, min;
  92.  
  93.     for (i=0; i<=n-2; i++) {
  94.         min = i;
  95.         for (j=i+1; j<=n-1; j++) {
  96.             if (a[j] < a[min])
  97.                 min = j;
  98.         }
  99.  
  100.         swap(a, min, i);
  101.     }
  102.  
  103. }
  104.  
  105. void swap(int *array, int a , int b){
  106.     int tmp;
  107.     tmp = array[a];
  108.     array[a] = array[b];
  109.     array[b] = tmp;
  110. }
  111.  
  112. void stampaRisRicBan(int *a, int elem, int n){
  113.  
  114.     int indice = 0;
  115.  
  116.     clock_t start, end;
  117.     double t;
  118.  
  119.     start = clock();
  120.     indice = ricercaBanale(a,elem,n);
  121.     end = clock();
  122.     t = ((double) (start)) / CLOCKS_PER_SEC;
  123.     printf("START: %lf END:%lf", (double)start, (double)end);
  124.     printf("\nDimensione vettore: %d \t Tempo impiegato per la ricerca: %lf secondi", n, t);
  125.  
  126.     if(ricercaBanale(a,elem,n) == -1)
  127.         printf("\tValore presente: NO\n");
  128.     else
  129.         printf("\tValore presente: SI\n");
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement