Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "analyze.h"
- #include "algorithm.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- //
- // Private
- //
- //
- // Public
- //
- /* jag behöver kolla vilken typ av array jag söker först
- (sorted,shuffle eller reverse), sen skapar jag arrayen genom
- malloc som jag skapar i benchmark, och stoppar in värden
- i olika forloopar beroende på vilken av dom det ska vara
- detta kommer vara för sorted
- for (int i = 0; i < SIZE; i++)
- {
- arr[i] = i;
- }
- för random
- for (int i = 0; i < SIZE; i++)
- {
- random = rand() % byte;
- arr[i] = random;
- }
- detta är för invers sortering:
- for (int i = 0; i < SIZE; i++)
- {
- arr[i] = byte - i;
- }
- }
- {
- */
- void worst_array(int *a, int SIZE)
- {
- for(int i = 0; i < SIZE; i++){
- a[i] = SIZE - i;
- }
- }
- void best_array(int *a, int SIZE)
- {
- for (int i = 0; i < SIZE; i++)
- {
- a[i] = i;
- }
- }
- void average_array(int *a, int SIZE)
- {
- for(int i = 0; i < SIZE; i++)
- {
- a[i]= rand() % (SIZE);
- }
- }
- void benchmark(const algorithm_t a, const case_t c, result_t *buf, int n)
- {
- int j, times = 20, wba;
- long int SIZE = SIZE_START;
- for(j = 0; j < ITERATIONS; j++){
- int *arr = (int*)malloc(SIZE*(sizeof(int)));
- clock_t start, end;
- double tot;
- int i;
- if(c == best_t ){
- best_array(arr, SIZE);
- }
- else if(c == worst_t){
- worst_array(arr, SIZE);
- wba = 0;
- }
- else {
- average_array(arr, SIZE);
- wba = 1;
- }
- if(a == bubble_sort_t){
- for(i = 0; i < times; i++){
- start = clock();
- bubble_sort(arr, SIZE);
- end = clock();
- tot = tot + (double)(end - start) / (double)CLOCKS_PER_SEC;
- if(wba == 0){
- worst_array(arr, SIZE);
- }
- else if(wba == 1){
- average_array(arr, SIZE);
- }
- }
- tot = tot / (double)(times);
- printf("%.6e\n", tot);
- buf[j].time = tot;
- }
- else if(a == quick_sort_t){
- for(i = 0; i < times; i++){
- start = clock();
- quick_sort(arr, SIZE);
- end = clock();
- tot = tot + (double)(end - start) / (double)CLOCKS_PER_SEC;
- if(wba == 0){
- worst_array(arr, SIZE);
- }
- else if(wba == 1){
- average_array(arr, SIZE);
- }
- }
- tot = tot / (double)(times);
- printf("%.6e\n", tot);
- buf[j].time = tot;
- }
- else if(a == insertion_sort_t)
- {
- start = clock();
- insertion_sort(arr, SIZE);
- end = clock();
- //tot = (double)(end - start) / (double)CLOCKS_PER_SEC;
- // printf("%.6e\n", tot);
- buf[j].time = (double)(end - start) / (double)CLOCKS_PER_SEC;
- }
- else if(a == linear_search_t){
- if(c == best_t){
- start = clock();
- linear_search(arr, SIZE, 0);
- end = clock();
- // tot = (double)(end - start) / (double)CLOCKS_PER_SEC;
- // printf("%.6e\n", tot);
- buf[j].time = (double)(end - start) / (double)CLOCKS_PER_SEC;
- }else if(c == worst_t){
- start = clock();
- linear_search(arr, SIZE, -1);
- end = clock();
- // tot = (double)(end - start) / (double)CLOCKS_PER_SEC;
- // printf("%.6e\n", tot);
- buf[j].time = (double)(end - start) / (double)CLOCKS_PER_SEC;
- }else if(c == average_t){
- start = clock();
- linear_search(arr, SIZE, arr[SIZE/2]);
- end = clock();
- // tot = (double)(end - start) / (double)CLOCKS_PER_SEC;
- // printf("%.6e\n", tot);
- buf[j].time = (double)(end - start) / (double)CLOCKS_PER_SEC;
- }
- }
- if(a == binary_search_t ){
- if(c == best_t){
- start = clock();
- binary_search(arr, SIZE, arr[SIZE/2]);
- end = clock();
- // tot = (double)(end - start) / (double)CLOCKS_PER_SEC;
- // printf("%.6e\n", tot);
- buf[j].time = (double)(end - start) / (double)CLOCKS_PER_SEC;
- }
- else if(c == worst_t){
- start = clock();
- linear_search(arr, SIZE, -1);
- end = clock();
- // tot = (double)(end - start) / (double)CLOCKS_PER_SEC;
- // printf("%.6e\n", tot);
- buf[j].time = (double)(end - start) / (double)CLOCKS_PER_SEC;
- }
- else if(c == average_t){
- }
- }
- buf[j].size = SIZE;
- SIZE = SIZE << 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement