Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <time.h>
- void stampa(int vett[], int lunghezza)
- {
- int i;
- for(i=0; i<lunghezza; i++)
- {
- printf("%d ", vett[i]);
- }
- printf("\n");
- }
- //https://italiancoders.it/ordinamento-di-un-array-parte-1/
- void selectionSort(int vett[], int lunghezza)
- {
- int i, j;
- for(i=0; i<lunghezza-1; i++)
- {
- for(j=i+1; j<lunghezza; j++)
- {
- if(vett[i] > vett[j])
- {
- int temp = vett[i];
- vett[i]=vett[j];
- vett[j]=temp;
- }
- }
- }
- }
- //https://italiancoders.it/ordinamento-di-un-array-parte-1/
- void bubbleSort(int vett[], int lunghezza)
- {
- bool scambio;
- int i,j;
- for(i=0; i<lunghezza; i++)
- {
- scambio = false;
- for(j=1; j<lunghezza-1 && scambio==false; j++)
- {
- if(vett[j-1] > vett[j])
- {
- int temp = vett[j-1];
- vett[j-1]=vett[j];
- vett[j]=temp;
- scambio = true;
- }
- }
- }
- }
- int main()
- {
- clock_t start, end;
- int l = 100000;
- int a[l];
- srand(time(NULL));
- int i;
- for(i=0; i<l; i++)
- {
- a[i] = (rand() % l*2) + 1;
- }
- //stampa(a,l);
- start = clock();
- //Ti ho messo 2 algoritmi di ordinamento base.
- //Prova a far partire il programma prima commentando il metodo bubbleSort, così verrà eseguito il selectionSort
- //noterai che su un'array di 100.000 elementi il tempo di esecuzione stampato sarà molto grande.
- //Ora commenta il selectionSort, e riesegui il programma, noterai un'incremento significativo del tempo di esecuzione.
- //Sopra ai 2 metodi ti ho lasciato un sito dove ti fa vedere anche con una animazione come funzionano i 2 algoritmi
- selectionSort(a,l);
- //bubbleSort(a,l);
- end = clock();
- double tempoImpiegato = (double)(end - start) / (double)CLOCKS_PER_SEC;
- printf("Tempo impiegato per eseguire un ordinamento: %lf secondi\n",tempoImpiegato);
- //stampa(a,l);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement