Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. #include "analyze.h"
  2. #include "algorithm.h"
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <time.h>
  6.  
  7.  
  8.  
  9. //
  10. // Private
  11. //
  12.  
  13. //
  14. // Public
  15. //
  16. /* jag behöver kolla vilken typ av array jag söker först
  17. (sorted,shuffle eller reverse), sen skapar jag arrayen genom
  18. malloc som jag skapar i benchmark, och stoppar in värden
  19. i olika forloopar beroende på vilken av dom det ska vara
  20.  
  21.  
  22. detta kommer vara för sorted
  23. for (int i = 0; i < SIZE; i++)
  24. {
  25. arr[i] = i;
  26. }
  27.  
  28. för random
  29. for (int i = 0; i < SIZE; i++)
  30. {
  31. random = rand() % byte;
  32. arr[i] = random;
  33. }
  34.  
  35. detta är för invers sortering:
  36. for (int i = 0; i < SIZE; i++)
  37. {
  38. arr[i] = byte - i;
  39. }
  40.  
  41.  
  42. }
  43.  
  44. {
  45. */
  46.  
  47. void worst_array(int *a, int SIZE)
  48. {
  49. for(int i = 0; i < SIZE; i++){
  50.  
  51. a[i] = SIZE - i;
  52. }
  53. }
  54. void best_array(int *a, int SIZE)
  55. {
  56. for (int i = 0; i < SIZE; i++)
  57. {
  58. a[i] = i;
  59. }
  60. }
  61. void average_array(int *a, int SIZE)
  62. {
  63. for(int i = 0; i < SIZE; i++)
  64. {
  65. a[i]= rand() % (SIZE);
  66. }
  67. }
  68.  
  69.  
  70.  
  71. void benchmark(const algorithm_t a, const case_t c, result_t *buf, int n)
  72. {
  73. int j, times = 20, wba;
  74. long int SIZE = SIZE_START;
  75. for(j = 0; j < ITERATIONS; j++){
  76. int *arr = (int*)malloc(SIZE*(sizeof(int)));
  77.  
  78. clock_t start, end;
  79. double tot;
  80. int i;
  81.  
  82.  
  83. if(c == best_t ){
  84. best_array(arr, SIZE);
  85.  
  86. }
  87. else if(c == worst_t){
  88. worst_array(arr, SIZE);
  89. wba = 0;
  90. }
  91. else {
  92. average_array(arr, SIZE);
  93. wba = 1;
  94.  
  95. }
  96.  
  97. if(a == bubble_sort_t){
  98. for(i = 0; i < times; i++){
  99. start = clock();
  100. bubble_sort(arr, SIZE);
  101. end = clock();
  102. tot = tot + (double)(end - start) / (double)CLOCKS_PER_SEC;
  103. if(wba == 0){
  104. worst_array(arr, SIZE);
  105. }
  106. else if(wba == 1){
  107. average_array(arr, SIZE);
  108. }
  109. }
  110. tot = tot / (double)(times);
  111.  
  112. printf("%.6e\n", tot);
  113. buf[j].time = tot;
  114.  
  115. }
  116. else if(a == quick_sort_t){
  117.  
  118.  
  119. for(i = 0; i < times; i++){
  120. start = clock();
  121. quick_sort(arr, SIZE);
  122. end = clock();
  123. tot = tot + (double)(end - start) / (double)CLOCKS_PER_SEC;
  124. if(wba == 0){
  125. worst_array(arr, SIZE);
  126. }
  127. else if(wba == 1){
  128. average_array(arr, SIZE);
  129. }
  130. }
  131. tot = tot / (double)(times);
  132.  
  133. printf("%.6e\n", tot);
  134. buf[j].time = tot;
  135.  
  136.  
  137. }
  138. else if(a == insertion_sort_t)
  139. {
  140. start = clock();
  141. insertion_sort(arr, SIZE);
  142. end = clock();
  143. //tot = (double)(end - start) / (double)CLOCKS_PER_SEC;
  144. // printf("%.6e\n", tot);
  145. buf[j].time = (double)(end - start) / (double)CLOCKS_PER_SEC;
  146. }
  147. else if(a == linear_search_t){
  148. if(c == best_t){
  149. start = clock();
  150. linear_search(arr, SIZE, 0);
  151. end = clock();
  152. // tot = (double)(end - start) / (double)CLOCKS_PER_SEC;
  153. // printf("%.6e\n", tot);
  154. buf[j].time = (double)(end - start) / (double)CLOCKS_PER_SEC;
  155.  
  156. }else if(c == worst_t){
  157. start = clock();
  158. linear_search(arr, SIZE, -1);
  159. end = clock();
  160. // tot = (double)(end - start) / (double)CLOCKS_PER_SEC;
  161. // printf("%.6e\n", tot);
  162. buf[j].time = (double)(end - start) / (double)CLOCKS_PER_SEC;
  163. }else if(c == average_t){
  164. start = clock();
  165. linear_search(arr, SIZE, arr[SIZE/2]);
  166. end = clock();
  167. // tot = (double)(end - start) / (double)CLOCKS_PER_SEC;
  168. // printf("%.6e\n", tot);
  169. buf[j].time = (double)(end - start) / (double)CLOCKS_PER_SEC;
  170. }
  171. }
  172. if(a == binary_search_t ){
  173.  
  174. if(c == best_t){
  175. start = clock();
  176. binary_search(arr, SIZE, arr[SIZE/2]);
  177. end = clock();
  178. // tot = (double)(end - start) / (double)CLOCKS_PER_SEC;
  179. // printf("%.6e\n", tot);
  180. buf[j].time = (double)(end - start) / (double)CLOCKS_PER_SEC;
  181. }
  182. else if(c == worst_t){
  183. start = clock();
  184. linear_search(arr, SIZE, -1);
  185. end = clock();
  186. // tot = (double)(end - start) / (double)CLOCKS_PER_SEC;
  187. // printf("%.6e\n", tot);
  188. buf[j].time = (double)(end - start) / (double)CLOCKS_PER_SEC;
  189. }
  190. else if(c == average_t){
  191.  
  192. }
  193. }
  194.  
  195. buf[j].size = SIZE;
  196. SIZE = SIZE << 1;
  197.  
  198.  
  199.  
  200. }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement