Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- typedef enum{ORDINATO, QUASI_ORDINATO, INV_ORDINATO, CASUALE} inputType;
- #define ORDINATO 0
- #define QUASI_ORDINATO 1
- #define INV_ORDINATO 2
- #define CASUALE 3
- int ricercaBanale (int *a, int numric, int dim);
- int* genera_array(int dimensione, inputType tipo_schema);
- void selectionSort(int *a, int n);
- void swap(int *array, int a , int b);
- void stampaRisRicBan(int *a, int elem, int n);
- int main() {
- srand(time(NULL));
- int elem, i;
- int *a;
- printf("\nChe elemento vuoi cercare?");
- scanf("%d",&elem);
- printf("\nRICERCA BANALE:\n");
- a = genera_array(100000, CASUALE);
- selectionSort(a,100000);
- stampaRisRicBan(a, elem, 100000);
- return 0;
- }
- int ricercaBanale (int *a, int numric, int dim){
- int pos = 0;
- do{
- if (numric == a[pos])
- return pos;
- else
- pos++;
- }while (pos < dim && a[pos] <= numric);
- return -1;
- }
- int* genera_array(int dimensione, inputType tipo_schema){
- int *array;
- int i, tmp;
- array = malloc(dimensione * sizeof(int));
- if (array == NULL)
- exit(-1);
- switch(tipo_schema){
- case ORDINATO:
- for(i=0;i<dimensione;i++)
- array[i] = i;
- break;
- case QUASI_ORDINATO:
- for(i=0;i<dimensione/2;i++)
- array[i] = i;
- for(i=dimensione/2;i<dimensione;i++)
- array[i] = rand();
- break;
- case INV_ORDINATO:
- tmp = dimensione-1;
- for(i=0;i<dimensione;i++) {
- array[i] = tmp;
- tmp--;
- }
- break;
- case CASUALE:
- for(i=0;i<dimensione;i++)
- array[i] = rand();
- break;
- }
- return array;
- }
- void selectionSort(int *a, int n){
- int i, j, min;
- for (i=0; i<=n-2; i++) {
- min = i;
- for (j=i+1; j<=n-1; j++) {
- if (a[j] < a[min])
- min = j;
- }
- swap(a, min, i);
- }
- }
- void swap(int *array, int a , int b){
- int tmp;
- tmp = array[a];
- array[a] = array[b];
- array[b] = tmp;
- }
- void stampaRisRicBan(int *a, int elem, int n){
- int indice = 0;
- clock_t start, end;
- double t;
- start = clock();
- indice = ricercaBanale(a,elem,n);
- end = clock();
- t = ((double) (start)) / CLOCKS_PER_SEC;
- printf("START: %lf END:%lf", (double)start, (double)end);
- printf("\nDimensione vettore: %d \t Tempo impiegato per la ricerca: %lf secondi", n, t);
- if(ricercaBanale(a,elem,n) == -1)
- printf("\tValore presente: NO\n");
- else
- printf("\tValore presente: SI\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement