Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #include "analyze.h"
  2. #include "algorithm.h"
  3.  
  4. //
  5. // Private
  6. //
  7.  
  8. //
  9. // Public
  10. //
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. void benchmark(const algorithm_t a, const case_t c, result_t *buf, int n)
  18. {
  19. int i, g, j;
  20. buf[0].size = SIZE_START;
  21. int x = 1;
  22. clock_t start, stop;
  23. srand(time(NULL));
  24.  
  25. int search = rand() % buf[g].size;
  26.  
  27.  
  28.  
  29. for(g=0;g<n;g++)
  30. {
  31. int *array = malloc((buf[g].size)*sizeof(int));
  32. if(c == best_t)
  33. {
  34.  
  35.  
  36. for(i=0;i<buf[g].size;i++)
  37. {
  38. array[i] = i;
  39. // printf("hej");
  40. // printf("%d ", array[i]);
  41. }
  42. }
  43.  
  44. else if(c == worst_t)
  45. {
  46. for(i=0;i<buf[g].size;i++)
  47. {
  48. array[i] = (buf[g].size - i);
  49. // printf("%d ", array[i]);
  50. }
  51. }
  52.  
  53. else if(c == average_t)
  54. {
  55. // srand(time(NULL));
  56. for(i=0;i<buf[g].size;i++)
  57. {
  58. array[i] = rand() % buf[g].size;
  59. // printf("%d ", array[i]);
  60.  
  61. }
  62. }
  63.  
  64. if(a == bubble_sort_t)
  65. {
  66. start = clock();
  67. bubble_sort(array, buf[g].size);
  68. }
  69.  
  70. else if(a == quick_sort_t)
  71. {
  72. start = clock();
  73. quick_sort(array, buf[g].size);
  74. }
  75.  
  76. else if(a == insertion_sort_t)
  77. {
  78. start = clock();
  79. insertion_sort(array, buf[g].size);
  80. }
  81.  
  82. else if(a == linear_search_t && c == best_t)
  83. {
  84. start = clock();
  85. linear_search(array, buf[g].size, 0);
  86. }
  87.  
  88. else if(a == linear_search_t && c == worst_t)
  89. {
  90. start = clock();
  91. linear_search(array, buf[g].size, -1);
  92. }
  93.  
  94. else if(a == linear_search_t && c == average_t)
  95. {
  96. start = clock();
  97. 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å?
  98. }
  99.  
  100. else if(a == binary_search_t && c == best_t)
  101. {
  102. start = clock();
  103. binary_search(array, buf[g].size, buf[g].size/2);
  104. }
  105.  
  106. else if(a == binary_search_t && c == worst_t)
  107. {
  108. quick_sort(array, buf[g].size);
  109. start = clock();
  110. binary_search(array, buf[g].size, -1);
  111. }
  112.  
  113. else if(a == binary_search_t && c == average_t)
  114. {
  115. quick_sort(array, buf[g].size);
  116. start = clock();
  117. binary_search(array, buf[g].size, search);//vad ska man söka efter? random number?
  118. }
  119.  
  120. stop = clock();
  121. buf[g].time = (double)(stop-start)/(double)CLOCKS_PER_SEC;
  122. buf[x].size = buf[g].size*2;
  123. x++;
  124. free(array);
  125. }
  126.  
  127.  
  128. if(x > n)
  129. {
  130. buf[x].size = NULL;
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement