Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.04 KB | None | 0 0
  1. //in this project we compare those 3 algorithms for sorting in all 3 cases:average, best and worst.The best case for
  2. //all of the algorithms the array must be sorted in ascendig order, for the worst case the array must be sorted in
  3. //descendig order and for the average case, we will use an unsorted array.From the graphs, we observe that in the best case, bubble sort
  4. //and selection sort have the same number of comparisons/assignments, and insertion sort has the same values in these cases.
  5. //The complexity of bubble sort for the best case is O(n), and for the average/worst case is O(n^2), insertion sort in the best case has the complexity O(n), and in
  6. //the /averageworst case is O(n^2), and selection sort has the complexity O(n^2) for all cases.
  7. #include <iostream>
  8. #include "Profiler.h"
  9.  
  10. #define AVG_BUBBLE_COMP "avg_bubble_comp"
  11. #define AVG_BUBBLE_ASSIG "avg_bubble_assig"
  12. #define AVG_BUBBLE_OP "avg_bubble_operations"
  13. #define AVG_INSERTION_OP "avg_insertion_operations"
  14.  
  15. #define AVG_SELECTION_COMP "avg_selection_comp"
  16. #define AVG_SELECTION_ASSIG "avg_selection_assig"
  17. #define AVG_SELECTION_OP "avg_selection_operations"
  18.  
  19. #define AVG_INSERTION_COMP "avg_insertion_comp"
  20. #define AVG_INSERTION_ASSIG "avg_insertion_assig"
  21. #define AVG_INSERTION_OP "avg_insertion_op"
  22.  
  23. #define BEST_BUBBLE_COMP "best_bubble_comp"
  24. #define BEST_BUBBLE_ASSIG "best_bubble_assig"
  25. #define BEST_BUBBLE_OP "best_bubble_op"
  26.  
  27. #define BEST_INSERTION_COMP "best_insertion_comp"
  28. #define BEST_INSERTION_ASSIG "best_insertion_assig"
  29. #define BEST_INSERTION_OP "best_insertion_op"
  30.  
  31. #define BEST_SELECTION_COMP "best_selection_comp"
  32. #define BEST_SELECTION_ASSIG "best_selection_assig"
  33. #define BEST_SELECTION_OP "best_selection_op"
  34.  
  35. #define WORST_BUBBLE_COMP "worst_bubble_comp"
  36. #define WORST_BUBBLE_ASSIG "worst_bubble_assig"
  37. #define WORST_BUBBLE_OP "worst_bubble_op"
  38.  
  39. #define WORST_SELECTION_COMP "worst_selection_comp"
  40. #define WORST_SELECTION_ASSIG "worst_selection_assig"
  41. #define WORST_SELECTION_OP "worst_selection_op"
  42.  
  43. #define WORST_INSERTION_COMP "worst_insertion_comp"
  44. #define WORST_INSERTION_ASSIG "worst_insertion_assig"
  45. #define WORST_INSERTION_OP "worst_insertion_op"
  46.  
  47. using namespace std;
  48. Profiler profiler("sort");
  49.  
  50. int comp_bubble, comp_insert, comp_sel, assig_bubble, assig_insert, assig_sel, op_bubble, op_insert, op_sel;
  51. void printArray(int* a, int n)
  52. {
  53. for (int i = 0; i < n; i++)
  54. cout << a[i] << " ";
  55. cout << "\n";
  56.  
  57. }
  58. void bubbleSort(int* a, int n)
  59. {
  60. assig_bubble = 0;
  61. comp_bubble = 0;
  62. int i, j, aux;
  63. for (i = 0; i < n; i++)
  64. {
  65. for (j = 0; j < n - i - 1; j++)
  66. {
  67. if (a[j] > a[j + 1])
  68. {
  69. aux = a[j];
  70. a[j] = a[j + 1];
  71. a[j + 1] = aux;
  72. assig_bubble += 3;
  73. }
  74. comp_bubble++;
  75. }
  76. }
  77. }
  78. void selectionSort(int* a, int n)
  79. {
  80. assig_sel = 0;
  81. comp_sel = 0;
  82. int buff, aux;
  83. for (int i = 0; i < n - 1; i++)//we go until n-1 because the last element is the biggest element from the array
  84. {
  85. buff = i;
  86. for (int j = i + 1; j < n; j++)
  87. {
  88. if (a[buff] > a[j])
  89. buff = j;
  90. comp_sel++;
  91. //here we find the minimum element from the unsorted array
  92. }
  93. if (buff != i)
  94. //here we compare if we changed the index earlier, and if yes, that means
  95. //that we found a smaller number than the number on the position i, and we swap them
  96. {
  97. aux = a[buff];
  98. a[buff] = a[i];
  99. a[i] = aux;
  100. assig_sel += 3;
  101. }
  102.  
  103. }
  104. }
  105. void insertionSort(int* a, int n)
  106. {
  107. assig_insert = 0;
  108. comp_insert = 0;
  109. int buff, j;
  110. for (int i = 1; i < n; i++)
  111. {
  112. buff = a[i];
  113. assig_insert++;
  114. j = i - 1;
  115. while (j >= 0 && buff < a[j])
  116. {
  117. //we shift to the right the element which is bigger than buff
  118. a[j + 1] = a[j];
  119. assig_insert++;
  120. j--;
  121. }
  122. if (j>=0)
  123. comp_insert++;
  124. a[j+1] = buff;
  125. assig_insert++;
  126. }
  127.  
  128. }
  129. void demoBubble()
  130. {
  131. cout << "Demo Bubble" << "\n";
  132. int a[5] = { 9,3,7,11,12 };
  133. bubbleSort(a, 5);
  134. printArray(a, 5);
  135. }
  136. void demoSelection()
  137. {
  138. cout << "\n" << "Demo Selection" << "\n";
  139. int a[5] = { 9,3,7,11,12 };
  140. selectionSort(a, 5);
  141. printArray(a, 5);
  142. }
  143. void demoInsertion()
  144. {
  145. int n = 5;
  146. cout << "\n" << "Demo Insertion" << "\n";
  147. int a[5] = { 9,3,7,11,12 };
  148. insertionSort(a, n);
  149. printArray(a, n);
  150.  
  151. }
  152. int* generateAvgCase(int n)
  153. {
  154. int* a = (int*)malloc(n * sizeof(int));
  155. FillRandomArray(a, n);
  156. return a;
  157. }
  158. int* generateAscendingOrder(int n)
  159. {
  160. int* a = (int*)malloc(n * sizeof(int));
  161. FillRandomArray(a, n, 10, 5000, false, 1); //array, size of the array, the interval of possible values, unique(not specified), sorted in ascending order
  162. return a;
  163. }
  164. int* generateDescendingOreder(int n)
  165. {
  166. int* a = (int*)malloc(n * sizeof(int));
  167. FillRandomArray(a, n, 10, 5000, false, 2);//array, size of the array, the interval of possible values, unique(not specified), sorted in descending order
  168. return a;
  169. }
  170.  
  171. void generateGraphs()
  172. {
  173. for (int size = 100; size <= 10000; size += 100)
  174. {
  175. ///average cases
  176. int* a = generateAvgCase(size);
  177. int* b = (int*)malloc(size * sizeof(int));
  178. for (int i = 0; i < size; i++)
  179. b[i] = a[i];
  180. //for the avg cases we have to use the same values, so we create a copy of a in b
  181. bubbleSort(b, size);
  182. profiler.countOperation(AVG_BUBBLE_COMP, size, comp_bubble);
  183. profiler.countOperation(AVG_BUBBLE_ASSIG, size, assig_bubble);
  184. profiler.addSeries(AVG_BUBBLE_OP, AVG_BUBBLE_COMP, AVG_BUBBLE_ASSIG);
  185.  
  186. for (int i = 0; i < size; i++)
  187. b[i] = a[i];
  188. selectionSort(b, size);
  189. profiler.countOperation(AVG_SELECTION_COMP, size, comp_sel);
  190. profiler.countOperation(AVG_SELECTION_ASSIG, size, assig_sel);//we create a separate graph because it can not be seen on the main graph
  191. profiler.addSeries(AVG_SELECTION_OP, AVG_SELECTION_COMP, AVG_SELECTION_ASSIG);
  192.  
  193. for (int i = 0; i < size; i++)
  194. b[i] = a[i];
  195. insertionSort(b, size);
  196. profiler.countOperation(AVG_INSERTION_COMP, size, comp_insert);//we create a separate graph because it can not be seen on the main graph
  197. profiler.countOperation(AVG_INSERTION_ASSIG, size, assig_insert);
  198. profiler.addSeries(AVG_INSERTION_OP, AVG_INSERTION_COMP, AVG_INSERTION_ASSIG);
  199. free(b);
  200. ///best cases
  201. a = generateAscendingOrder(size);
  202. bubbleSort(a, size);
  203. profiler.countOperation(BEST_BUBBLE_COMP, size, comp_bubble);
  204. profiler.countOperation(BEST_BUBBLE_ASSIG, size, assig_bubble);
  205. profiler.addSeries(BEST_BUBBLE_OP, BEST_BUBBLE_COMP, BEST_BUBBLE_ASSIG);
  206. free(a);
  207.  
  208. a = generateAscendingOrder(size);
  209. selectionSort(a, size);
  210. profiler.countOperation(BEST_SELECTION_COMP, size, comp_sel);
  211. profiler.countOperation(BEST_SELECTION_ASSIG, size, assig_sel);
  212. profiler.addSeries(BEST_SELECTION_OP, BEST_SELECTION_COMP, BEST_SELECTION_ASSIG);
  213. free(a);
  214.  
  215. a = generateAscendingOrder(size);
  216. insertionSort(a, size);
  217. profiler.countOperation(BEST_INSERTION_COMP, size, comp_insert);//we create a separate graph because it can not be seen on the main graph
  218. profiler.countOperation(BEST_INSERTION_ASSIG, size, assig_insert);
  219. profiler.addSeries(BEST_INSERTION_OP, BEST_INSERTION_COMP, BEST_INSERTION_ASSIG);//we create a separate graph because it can not be seen on the main graph
  220. free(a);
  221. ///worst cases
  222. a = generateDescendingOreder(size);
  223. bubbleSort(a, size);
  224. profiler.countOperation(WORST_BUBBLE_COMP, size, comp_bubble);
  225. profiler.countOperation(WORST_BUBBLE_ASSIG, size, assig_bubble);
  226. profiler.addSeries(WORST_BUBBLE_OP, WORST_BUBBLE_COMP, WORST_BUBBLE_ASSIG);
  227. free(a);
  228.  
  229. a = generateDescendingOreder(size);
  230. selectionSort(a, size);
  231. profiler.countOperation(WORST_SELECTION_COMP, size, comp_sel);
  232. profiler.countOperation(WORST_SELECTION_ASSIG, size, assig_sel);//we create a separate graph because it can not be seen on the main graph
  233. profiler.addSeries(WORST_SELECTION_OP, WORST_SELECTION_COMP, WORST_SELECTION_ASSIG);
  234. free(a);
  235.  
  236. a = generateDescendingOreder(size);
  237. insertionSort(a, size);
  238. profiler.countOperation(WORST_INSERTION_COMP, size, comp_insert);//we create a separate graph because it can not be seen on the main graph
  239. profiler.countOperation(WORST_INSERTION_ASSIG, size, assig_insert);
  240. profiler.addSeries(WORST_INSERTION_OP, WORST_INSERTION_COMP, WORST_INSERTION_ASSIG);
  241. free(a);
  242.  
  243. profiler.createGroup("Average Case Comp", AVG_BUBBLE_COMP, AVG_SELECTION_COMP);
  244. profiler.createGroup("Average Case Assig", AVG_BUBBLE_ASSIG, AVG_INSERTION_ASSIG);
  245. profiler.createGroup("Average Case Op", AVG_BUBBLE_OP, AVG_SELECTION_OP, AVG_INSERTION_OP);
  246.  
  247. profiler.createGroup("Best Case Comp", BEST_BUBBLE_COMP, BEST_SELECTION_COMP);
  248. profiler.createGroup("Best Case Assig", BEST_BUBBLE_ASSIG, BEST_SELECTION_ASSIG, BEST_INSERTION_ASSIG);
  249. profiler.createGroup("Best Case Op", BEST_BUBBLE_OP, BEST_SELECTION_OP);
  250.  
  251. profiler.createGroup("Worst Case Comp", WORST_BUBBLE_COMP, WORST_SELECTION_COMP);
  252. profiler.createGroup("Worst Case Assig", WORST_BUBBLE_ASSIG, WORST_INSERTION_ASSIG);
  253. profiler.createGroup("Worst Case Op", WORST_BUBBLE_OP, WORST_SELECTION_OP, WORST_INSERTION_OP);
  254.  
  255. }
  256. }
  257. int main()
  258. {
  259. demoBubble();
  260. demoInsertion();
  261. demoSelection();
  262. generateGraphs();
  263. profiler.showReport();
  264.  
  265. return 0;
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement