Advertisement
Guest User

Untitled

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