Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "analyze.h"
- #include "algorithm.h"
- //
- // Private
- //
- //
- // Public
- //
- void benchmark(const algorithm_t a, const case_t c, result_t *buf, int n)
- {
- int i, g, j;
- buf[0].size = SIZE_START;
- int x = 1;
- clock_t start, stop;
- srand(time(NULL));
- int search = rand() % buf[g].size;
- for(g=0;g<n;g++)
- {
- int *array = malloc((buf[g].size)*sizeof(int));
- if(c == best_t)
- {
- for(i=0;i<buf[g].size;i++)
- {
- array[i] = i;
- // printf("hej");
- // printf("%d ", array[i]);
- }
- }
- else if(c == worst_t)
- {
- for(i=0;i<buf[g].size;i++)
- {
- array[i] = (buf[g].size - i);
- // printf("%d ", array[i]);
- }
- }
- else if(c == average_t)
- {
- // srand(time(NULL));
- for(i=0;i<buf[g].size;i++)
- {
- array[i] = rand() % buf[g].size;
- // printf("%d ", array[i]);
- }
- }
- if(a == bubble_sort_t)
- {
- start = clock();
- bubble_sort(array, buf[g].size);
- }
- else if(a == quick_sort_t)
- {
- start = clock();
- quick_sort(array, buf[g].size);
- }
- else if(a == insertion_sort_t)
- {
- start = clock();
- insertion_sort(array, buf[g].size);
- }
- else if(a == linear_search_t && c == best_t)
- {
- start = clock();
- linear_search(array, buf[g].size, 0);
- }
- else if(a == linear_search_t && c == worst_t)
- {
- start = clock();
- linear_search(array, buf[g].size, -1);
- }
- else if(a == linear_search_t && c == average_t)
- {
- start = clock();
- linear_search(array, buf[g].size, search); //tänker att sökvärdet blir det värdet som ligger i mitten av arrayen för ett average case, elelr kanske random här också?
- }
- else if(a == binary_search_t && c == best_t)
- {
- start = clock();
- binary_search(array, buf[g].size, buf[g].size/2);
- }
- else if(a == binary_search_t && c == worst_t)
- {
- quick_sort(array, buf[g].size);
- start = clock();
- binary_search(array, buf[g].size, -1);
- }
- else if(a == binary_search_t && c == average_t)
- {
- quick_sort(array, buf[g].size);
- start = clock();
- binary_search(array, buf[g].size, search);//vad ska man söka efter? random number?
- }
- stop = clock();
- buf[g].time = (double)(stop-start)/(double)CLOCKS_PER_SEC;
- buf[x].size = buf[g].size*2;
- x++;
- free(array);
- }
- if(x > n)
- {
- buf[x].size = NULL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement