Proyecto de ordenamiento para el curso de Análisis y Diseño de Algoritmos.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

292 lines
7.9 KiB

  1. /*
  2. * Copyright 2018 Christopher Cromer
  3. * Copyright 2018 Rodolfo Cuevas
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  8. *
  9. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  10. *
  11. * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <getopt.h>
  18. #include <string.h>
  19. #include "random.h"
  20. #include "timer.h"
  21. #include "bubble_sort.h"
  22. #include "count_sort.h"
  23. #include "quick_sort.h"
  24. #include "merge_sort.h"
  25. #define SORT_VERSION "1.0.0"
  26. /**
  27. * El array a ordenar
  28. */
  29. static int *unordered_array;
  30. static int *work_array;
  31. /**
  32. * Imprimir el uso del programa
  33. */
  34. void print_usage() {
  35. fprintf(stdout, "uso: sort [OPCIÓN]\n");
  36. fprintf(stdout, " -a, --all usar todos los algoritmos de ordenamentio\n");
  37. fprintf(stdout, " -m, --merge usar merge sort\n");
  38. fprintf(stdout, " -q, --quick usar quick sort\n");
  39. fprintf(stdout, " -b, --bubble usar bubble sort\n");
  40. fprintf(stdout, " -B, --bitonic usar bitonic sort\n");
  41. fprintf(stdout, " -c, --count usar ordenamiento por conteo\n");
  42. fprintf(stdout, " -s, --selection usar ordenamiento por selección\n");
  43. fprintf(stdout, " -n, --n=N la cantidad de elementos a ordenar, la\n");
  44. fprintf(stdout, " cantidad predeterminado es 10\n");
  45. fprintf(stdout, " -e, --elegir el usuario debe elegir los \"n\" valores de\n");
  46. fprintf(stdout, " elementos a ordenar, sin esta opción los\n");
  47. fprintf(stdout, " valores son elegido por el programa al azar\n");
  48. fprintf(stdout, " -i, --imprimir imprimir el array antes y despues de ordenar\n");
  49. fprintf(stdout, " -v, --version mostrar la versión del programa\n");
  50. }
  51. /**
  52. * Imprimir un array
  53. * @param *array El array a imprimir
  54. * @param n La cantidad de elementos que están en el array
  55. */
  56. void print_array(int *array, int n) {
  57. int i;
  58. for (i = 0; i < n; i++) {
  59. fprintf(stdout, "%d ", array[i]);
  60. }
  61. fprintf(stdout, "\n\n");
  62. }
  63. /**
  64. * Leer el buffer de stdin y guardar el valor si es numerico
  65. * @param variable Donde se guarda el valor del stdin
  66. * @return Retorna 1 si es exitosa ó 0 si falla
  67. */
  68. int read_buffer(int *variable) {
  69. char buffer[12];
  70. while (1) {
  71. if (fgets(buffer, 12, stdin) != NULL) {
  72. if (buffer[strlen(buffer) - 1] == '\n') {
  73. buffer[strlen(buffer) - 1] = '\0';
  74. break;
  75. }
  76. }
  77. }
  78. char **check = malloc(sizeof(char**));
  79. if (check == NULL) {
  80. fprintf(stderr, "Error: Out of heap space!\n");
  81. exit(5);
  82. }
  83. long input = strtol(buffer, check, 10);
  84. if (*check[0] == '\0') {
  85. free(check);
  86. *variable = (int) input;
  87. return 1;
  88. }
  89. else {
  90. free(check);
  91. return 0;
  92. }
  93. }
  94. /**
  95. * Liberar la memoria al salir
  96. */
  97. void cleanup() {
  98. free(unordered_array);
  99. free(work_array);
  100. }
  101. /**
  102. * La entrada del programa
  103. * @param argc La cantidad de argumentos pasado al programa
  104. * @return Retorna el codigo de error o 0 por exito
  105. */
  106. int main (int argc, char **argv) {
  107. long long i;
  108. int n = 10;
  109. int elegir = 0;
  110. int imprimir = 0;
  111. int merge = 0;
  112. int quick = 0;
  113. int bubble = 0;
  114. int bitonic = 0;
  115. int count = 0;
  116. int selection = 0;
  117. int opt;
  118. int long_index = 0;
  119. static struct option long_options[] = {
  120. {"all", no_argument, 0, 'a'},
  121. {"merge", no_argument, 0, 'm'},
  122. {"quick", no_argument, 0, 'q'},
  123. {"bubble", no_argument, 0, 'b'},
  124. {"bitonic", no_argument, 0, 'B'},
  125. {"count", no_argument, 0, 'c'},
  126. {"selection", no_argument, 0, 's'},
  127. {"n", required_argument, 0, 'n'},
  128. {"elegir", no_argument, 0, 'e'},
  129. {"imprimir", no_argument, 0, 'i'},
  130. {"version", no_argument, 0, 'v'},
  131. {0, 0, 0, 0}
  132. };
  133. if (argc == 1) {
  134. print_usage();
  135. return 0;
  136. }
  137. while ((opt = getopt_long(argc, argv, "amqbBcsn:eiv", long_options, &long_index)) != -1) {
  138. switch (opt) {
  139. case 'a':
  140. merge = 1;
  141. quick = 1;
  142. bubble = 1;
  143. bitonic = 1;
  144. count = 1;
  145. selection = 1;
  146. break;
  147. case 'm':
  148. merge = 1;
  149. break;
  150. case 'q':
  151. quick = 1;
  152. break;
  153. case 'b':
  154. bubble = 1;
  155. break;
  156. case 'B':
  157. bitonic = 1;
  158. break;
  159. case 'c':
  160. count = 1;
  161. break;
  162. case 's':
  163. selection = 1;
  164. break;
  165. case 'n':
  166. n = atol(optarg);
  167. if (n <= 1) {
  168. fprintf(stderr, "Error: n tiene que ser mayor de 1!\n");
  169. return 3;
  170. }
  171. break;
  172. case 'e':
  173. elegir = 1;
  174. break;
  175. case 'i':
  176. imprimir = 1;
  177. break;
  178. case 'v':
  179. printf("sort versión: %s\n", SORT_VERSION);
  180. return 0;
  181. break;
  182. default:
  183. print_usage();
  184. return 1;
  185. }
  186. }
  187. if (!merge && !quick && !bubble && !bitonic && !count && !selection) {
  188. fprintf(stderr, "Error: No se seleccionó un algoritmo valido!\n");
  189. print_usage();
  190. return 4;
  191. }
  192. unordered_array = malloc(sizeof(int) * n);
  193. if (unordered_array == NULL) {
  194. fprintf(stderr, "Error: Out of heap space!\n");
  195. exit(5);
  196. }
  197. work_array = malloc(sizeof(int) * n);
  198. if (work_array == NULL) {
  199. fprintf(stderr, "Error: Out of heap space!\n");
  200. exit(5);
  201. }
  202. atexit(cleanup);
  203. // Llenar el array con valores para ordenar después
  204. for (i = 0; i < n; i++) {
  205. if (elegir) {
  206. opt = 0;
  207. fprintf(stdout, "Elegir elemento %lli: ", i + 1);
  208. while (!read_buffer(&opt)) {
  209. fprintf(stdout, "Número invalido! Tiene que ser mayor de 1!\n");
  210. fprintf(stdout, "Elegir elemento %lli: ", i + 1);
  211. }
  212. unordered_array[i] = opt;
  213. }
  214. else {
  215. unordered_array[i] = gen_rand((n * 10) * -1, n * 10);
  216. }
  217. }
  218. if (merge) {
  219. fprintf(stdout, "Merge sort corriendo... ");
  220. fflush(stdout);
  221. memcpy(work_array, unordered_array, sizeof(int) * n);
  222. start_timer();
  223. merge_sort(work_array, n);
  224. stop_timer();
  225. fprintf(stdout, "done\n");
  226. print_timer();
  227. }
  228. if (quick) {
  229. fprintf(stdout, "Quick sort corriendo... ");
  230. fflush(stdout);
  231. memcpy(work_array, unordered_array, sizeof(int) * n);
  232. start_timer();
  233. quick_sort(work_array, n);
  234. stop_timer();
  235. fprintf(stdout, "done\n");
  236. print_timer();
  237. }
  238. if (bubble) {
  239. fprintf(stdout, "Bubble sort corriendo... ");
  240. fflush(stdout);
  241. memcpy(work_array, unordered_array, sizeof(int) * n);
  242. start_timer();
  243. bubble_sort(work_array, n);
  244. stop_timer();
  245. fprintf(stdout, "done\n");
  246. print_timer();
  247. }
  248. if (bitonic) {
  249. // bitonic sort
  250. }
  251. if (count) {
  252. fprintf(stdout, "Count sort corriendo... ");
  253. fflush(stdout);
  254. memcpy(work_array, unordered_array, sizeof(int) * n);
  255. start_timer();
  256. count_sort(work_array, n);
  257. stop_timer();
  258. fprintf(stdout, "done\n");
  259. print_timer();
  260. }
  261. if (selection) {
  262. // selection sort
  263. }
  264. if (imprimir) {
  265. fprintf(stdout, "\nAntes:\n");
  266. print_array(unordered_array, n);
  267. fprintf(stdout, "\nDespués:\n");
  268. print_array(work_array, n);
  269. }
  270. return 0;
  271. }