Guest User

Code

a guest
Nov 15th, 2025
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. typedef struct {
  6. int swaps;
  7. int calls;
  8. } Counters;
  9.  
  10. typedef struct {
  11. char name[3];
  12. Counters c;
  13. } Method;
  14.  
  15. // ===================== PIVOT AUX =====================
  16. int pivotMedian3(int *array, int start, int end) {
  17. int n = end - start + 1;
  18. int i1 = start + n / 4;
  19. int i2 = start + n / 2;
  20. int i3 = start + 3 * n / 4;
  21. int a = array[i1], b = array[i2], cval = array[i3];
  22. if ((a >= b && a <= cval) || (a <= b && a >= cval)) return i1;
  23. else if ((b >= a && b <= cval) || (b <= a && b >= cval)) return i2;
  24. else return i3;
  25. }
  26.  
  27. int pivotRandom(int start, int end) {
  28. return start + rand() % (end - start + 1);
  29. }
  30.  
  31. // ===================== LOMUTO =====================
  32. int lomuto(int *array, int start, int end, Counters *c) {
  33. int pivot = array[end], i = start - 1;
  34. for (int j = start; j < end; j++) {
  35. if (array[j] <= pivot) {
  36. i++;
  37. int tmp = array[i]; array[i] = array[j]; array[j] = tmp;
  38. c->swaps++;
  39. }
  40. }
  41. int tmp = array[i + 1]; array[i + 1] = array[end]; array[end] = tmp; c->swaps++;
  42. return i + 1;
  43. }
  44.  
  45. int lomutoMedian3(int *array, int start, int end, Counters *c) {
  46. int pivotIndex = pivotMedian3(array, start, end);
  47. if (pivotIndex != end) { int tmp = array[pivotIndex]; array[pivotIndex] = array[end]; array[end] = tmp; c->swaps++; }
  48. return lomuto(array, start, end, c);
  49. }
  50.  
  51. int lomutoRandom(int *array, int start, int end, Counters *c) {
  52. int pivotIndex = pivotRandom(start, end);
  53. int tmp = array[end]; array[end] = array[pivotIndex]; array[pivotIndex] = tmp; c->swaps++;
  54. return lomuto(array, start, end, c);
  55. }
  56.  
  57. void quickSortLomutoStandard(int *array, int start, int end, Counters *c) {
  58. c->calls++;
  59. if (start < end) {
  60. int p = lomuto(array, start, end, c);
  61. quickSortLomutoStandard(array, start, p - 1, c);
  62. quickSortLomutoStandard(array, p + 1, end, c);
  63. }
  64. }
  65.  
  66. void quickSortLomutoMedian3(int *array, int start, int end, Counters *c) {
  67. c->calls++;
  68. if (start < end) {
  69. int p = lomutoMedian3(array, start, end, c);
  70. quickSortLomutoMedian3(array, start, p - 1, c);
  71. quickSortLomutoMedian3(array, p + 1, end, c);
  72. }
  73. }
  74.  
  75. void quickSortLomutoRandom(int *array, int start, int end, Counters *c) {
  76. c->calls++;
  77. if (start < end) {
  78. int p = lomutoRandom(array, start, end, c);
  79. quickSortLomutoRandom(array, start, p - 1, c);
  80. quickSortLomutoRandom(array, p + 1, end, c);
  81. }
  82. }
  83.  
  84. // ===================== HOARE =====================
  85. int hoare(int *array, int start, int end, Counters *c) {
  86. int pivot = array[start], i = start - 1, j = end + 1;
  87. while (1) {
  88. do { j--; } while (array[j] > pivot);
  89. do { i++; } while (array[i] < pivot);
  90. if (i < j) { int tmp = array[i]; array[i] = array[j]; array[j] = tmp; c->swaps++; }
  91. else return j;
  92. }
  93. }
  94.  
  95. int hoareMedian3(int *array, int start, int end, Counters *c) {
  96. int pivotIndex = pivotMedian3(array, start, end);
  97. if (pivotIndex != start) { int tmp = array[start]; array[start] = array[pivotIndex]; array[pivotIndex] = tmp; c->swaps++; }
  98. return hoare(array, start, end, c);
  99. }
  100.  
  101. int hoareRandom(int *array, int start, int end, Counters *c) {
  102. int pivotIndex = pivotRandom(start, end);
  103. int tmp = array[start]; array[start] = array[pivotIndex]; array[pivotIndex] = tmp; c->swaps++;
  104. return hoare(array, start, end, c);
  105. }
  106.  
  107. void quickSortHoareStandard(int *array, int start, int end, Counters *c) {
  108. c->calls++;
  109. if (start < end) {
  110. int p = hoare(array, start, end, c);
  111. quickSortHoareStandard(array, start, p, c);
  112. quickSortHoareStandard(array, p + 1, end, c);
  113. }
  114. }
  115.  
  116. void quickSortHoareMedian3(int *array, int start, int end, Counters *c) {
  117. c->calls++;
  118. if (start < end) {
  119. int p = hoareMedian3(array, start, end, c);
  120. quickSortHoareMedian3(array, start, p, c);
  121. quickSortHoareMedian3(array, p + 1, end, c);
  122. }
  123. }
  124.  
  125. void quickSortHoareRandom(int *array, int start, int end, Counters *c) {
  126. c->calls++;
  127. if (start < end) {
  128. int p = hoareRandom(array, start, end, c);
  129. quickSortHoareRandom(array, start, p, c);
  130. quickSortHoareRandom(array, p + 1, end, c);
  131. }
  132. }
  133.  
  134. // ===================== UTIL =====================
  135. void copyArray(int *src, int *dest, int n) {
  136. for (int i = 0; i < n; i++) dest[i] = src[i];
  137. }
  138.  
  139. // ===================== MAIN =====================
  140. int main(int argc, char *argv[]) {
  141. if (argc < 3) {
  142. printf("Uso: %s <input> <output>\n", argv[0]);
  143. return 1;
  144. }
  145.  
  146. srand((unsigned int)time(NULL));
  147.  
  148. FILE *fin = fopen(argv[1], "r");
  149. if (!fin) { perror("Erro ao abrir input"); return 1; }
  150.  
  151. FILE *fout = fopen(argv[2], "w");
  152. if (!fout) { perror("Erro ao abrir output"); fclose(fin); return 1; }
  153.  
  154. int totalArrays;
  155. fscanf(fin, "%d", &totalArrays);
  156.  
  157. for (int t = 0; t < totalArrays; t++) {
  158. int size;
  159. fscanf(fin, "%d", &size);
  160.  
  161. int *original = malloc(size * sizeof(int));
  162. int *array = malloc(size * sizeof(int));
  163.  
  164. for (int i = 0; i < size; i++) fscanf(fin, "%d", &original[i]);
  165.  
  166. Method methods[6] = { {"LP"}, {"LM"}, {"LA"}, {"HP"}, {"HM"}, {"HA"} };
  167.  
  168. copyArray(original, array, size); quickSortLomutoStandard(array, 0, size - 1, &methods[0].c);
  169. copyArray(original, array, size); quickSortLomutoMedian3(array, 0, size - 1, &methods[1].c);
  170. copyArray(original, array, size); quickSortLomutoRandom(array, 0, size - 1, &methods[2].c);
  171. copyArray(original, array, size); quickSortHoareStandard(array, 0, size - 1, &methods[3].c);
  172. copyArray(original, array, size); quickSortHoareMedian3(array, 0, size - 1, &methods[4].c);
  173. copyArray(original, array, size); quickSortHoareRandom(array, 0, size - 1, &methods[5].c);
  174.  
  175. // ordena pelo total
  176. for (int i = 1; i < 6; i++) {
  177. Method key = methods[i];
  178. int keyVal = key.c.calls + key.c.swaps;
  179. int j = i - 1;
  180. while (j >= 0 && (methods[j].c.calls + methods[j].c.swaps) > keyVal) {
  181. methods[j + 1] = methods[j];
  182. j--;
  183. }
  184. methods[j + 1] = key;
  185. }
  186.  
  187. fprintf(fout, "[%d]:", size);
  188. for (int i = 0; i < 6; i++) {
  189. fprintf(fout, "%s(%d)%s", methods[i].name, methods[i].c.calls + methods[i].c.swaps, (i < 5 ? "," : ""));
  190. }
  191. fprintf(fout, "\n");
  192.  
  193. free(original);
  194. free(array);
  195. }
  196.  
  197. fclose(fin);
  198. fclose(fout);
  199. return 0;
  200. }
  201.  
Advertisement
Add Comment
Please, Sign In to add comment