From a43a470e7f1f17ce869f24a6e5e8e87eeca95e28 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Tue, 13 Nov 2018 11:27:16 -0300 Subject: [PATCH] allow all algorithms to be run or a mix of them --- src/sort.c | 110 +++++++++++++++++++++++++--------------------------- src/timer.c | 19 +++++---- 2 files changed, 64 insertions(+), 65 deletions(-) diff --git a/src/sort.c b/src/sort.c index 976b696..551806c 100644 --- a/src/sort.c +++ b/src/sort.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "random.h" #include "timer.h" #include "bubble_sort.h" @@ -26,13 +27,15 @@ /** * El array a ordenar */ -static int *array; +static int *unordered_array; +static int *work_array; /** * Imprimir el uso del programa */ void print_usage() { fprintf(stdout, "uso: sort [OPCIÓN]\n"); + fprintf(stdout, " -a, --all usar todos los algoritmos de ordenamentio\n"); fprintf(stdout, " -m, --merge usar merge sort\n"); fprintf(stdout, " -q, --quick usar quick sort\n"); fprintf(stdout, " -b, --bubble usar bubble sort\n"); @@ -48,15 +51,6 @@ void print_usage() { fprintf(stdout, " -v, --version mostrar la versión del programa\n"); } -/** - * Imprimir un mensaje de error y salir - */ -void extra_algorithm() { - fprintf(stderr, "Error: Solo se puede elegir un algoritmo al vez!\n"); - print_usage(); - exit(2); -} - /** * Imprimir un array * @param *array El array a imprimir @@ -74,7 +68,8 @@ void print_array(int *array, int n) { * Liberar la memoria al salir */ void cleanup() { - free(array); + free(unordered_array); + free(work_array); } /** @@ -87,10 +82,16 @@ int main (int argc, char **argv) { int n = 10; int elegir = 0; int imprimir = 0; - int algoritmo = -1; + int merge = 0; + int quick = 0; + int bubble = 0; + int bitonic = 0; + int count = 0; + int selection = 0; int opt; int long_index = 0; static struct option long_options[] = { + {"all", no_argument, 0, 'a'}, {"merge", no_argument, 0, 'm'}, {"quick", no_argument, 0, 'q'}, {"bubble", no_argument, 0, 'b'}, @@ -109,43 +110,33 @@ int main (int argc, char **argv) { return 0; } - while ((opt = getopt_long(argc, argv, "mqbBcsn:eiv", long_options, &long_index)) != -1) { + while ((opt = getopt_long(argc, argv, "amqbBcsn:eiv", long_options, &long_index)) != -1) { switch (opt) { + case 'a': + merge = 1; + quick = 1; + bubble = 1; + bitonic = 1; + count = 1; + selection = 1; + break; case 'm': - if (algoritmo != -1) { - extra_algorithm(); - } - algoritmo = 0; + merge = 1; break; case 'q': - if (algoritmo != -1) { - extra_algorithm(); - } - algoritmo = 1; + quick = 1; break; case 'b': - if (algoritmo != -1) { - extra_algorithm(); - } - algoritmo = 2; + bubble = 1; break; case 'B': - if (algoritmo != -1) { - extra_algorithm(); - } - algoritmo = 3; + bitonic = 1; break; case 'c': - if (algoritmo != -1) { - extra_algorithm(); - } - algoritmo = 4; + count = 1; break; case 's': - if (algoritmo != -1) { - extra_algorithm(); - } - algoritmo = 5; + selection = 1; break; case 'n': n = atol(optarg); @@ -170,13 +161,14 @@ int main (int argc, char **argv) { } } - if (algoritmo < 0 || algoritmo > 5) { + if (!merge && !quick && !bubble && !bitonic && !count && !selection) { fprintf(stderr, "Error: No se seleccionó un algoritmo valido!\n"); print_usage(); return 4; } - array = malloc(sizeof(int) * n); + unordered_array = malloc(sizeof(int) * n); + work_array = malloc(sizeof(int) * n); atexit(cleanup); // Llenar el array con valores para ordenar después @@ -190,50 +182,54 @@ int main (int argc, char **argv) { fprintf(stdout, "Número invalido! Tiene que ser mayor de 1!\n"); fprintf(stdout, "Elegir elemento %lli: ", i + 1); } - array[i] = opt; + unordered_array[i] = opt; } else { - array[i] = gen_rand((n * 10) * -1, n * 10); + unordered_array[i] = gen_rand((n * 10) * -1, n * 10); } } - if (imprimir) { - fprintf(stdout, "Antes:\n"); - print_array(array, n); - } - - if (algoritmo == 0) { + if (merge) { // merge sort } - else if (algoritmo == 1) { + + if (quick) { // quick sort } - else if (algoritmo == 2) { + + if (bubble) { fprintf(stdout, "Bubble sort corriendo...\n"); fflush(stdout); + memcpy(work_array, unordered_array, sizeof(int) * n); start_timer(); - bubble_sort(array, n); + bubble_sort(work_array, n); stop_timer(); + print_timer(); } - else if (algoritmo == 3) { + + if (bitonic) { // bitonic sort } - else if (algoritmo == 4) { + + if (count) { fprintf(stdout, "Count sort corriendo...\n"); fflush(stdout); + memcpy(work_array, unordered_array, sizeof(int) * n); start_timer(); - count_sort(array, n); + count_sort(work_array, n); stop_timer(); + print_timer(); } - else if (algoritmo == 5) { + + if (selection) { // selection sort } if (imprimir) { + fprintf(stdout, "\nAntes:\n"); + print_array(unordered_array, n); fprintf(stdout, "\nDespués:\n"); - print_array(array, n); + print_array(work_array, n); } - - print_timer(); return 0; } diff --git a/src/timer.c b/src/timer.c index 014633c..f7611fc 100644 --- a/src/timer.c +++ b/src/timer.c @@ -30,13 +30,8 @@ static time_t stop_time = 0; * Empezar el timer */ void start_timer() { - if (start_time != 0) { - fprintf(stderr, "Error: El temporizador ya comenzó!\n"); - } - else { - time(&start_time); - stop_time = 0; - } + time(&start_time); + stop_time = 0; } /** @@ -55,7 +50,15 @@ void stop_timer() { * Imprimir el tiempo de ejecución */ void print_timer() { - fprintf(stdout, "Tiempo de ejecución: %ld segundos\n", (long int) (stop_time - start_time)); + if (start_time == 0) { + fprintf(stderr, "Error: El temporizador no ha comenzado todavía!\n"); + } + else if (stop_time == 0) { + fprintf(stderr, "Error: El temporizador no ha terminado todavía!\n"); + } + else { + fprintf(stdout, "Tiempo de ejecución: %ld segundos\n", (long int) (stop_time - start_time)); + } } /**