From ca7e0a9be84024a89d9e7529017db4a7e9e4c3b6 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 18 Nov 2018 17:57:59 -0300 Subject: [PATCH] various improvements --- Makefile | 4 ++-- src/bitonic_sort.c | 9 +++++---- src/count_sort.c | 1 + src/include/swap.h | 19 ------------------ src/selection_sort.c | 9 +++++---- src/sort.c | 14 ++++++------- src/swap.c | 28 -------------------------- src/timer.c | 48 ++++++++++++++++++-------------------------- test/Makefile | 2 +- 9 files changed, 41 insertions(+), 93 deletions(-) delete mode 100644 src/include/swap.h delete mode 100644 src/swap.c diff --git a/Makefile b/Makefile index 9cd7669..df27027 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC=gcc CFLAGS=-Wall -Isrc/include -DDEBUG -g -#LDFLAGS=-lm -SRC=src/sort.c src/random.c src/swap.c src/bubble_sort.c src/timer.c src/count_sort.c src/quick_sort.c src/merge_sort.c src/bitonic_sort.c src/selection_sort.c +LDFLAGS=-lm +SRC=src/sort.c src/random.c src/bubble_sort.c src/timer.c src/count_sort.c src/quick_sort.c src/merge_sort.c src/bitonic_sort.c src/selection_sort.c OBJ=$(SRC:.c=.o) all: sort informe diff --git a/src/bitonic_sort.c b/src/bitonic_sort.c index e8fe6a9..61751cb 100644 --- a/src/bitonic_sort.c +++ b/src/bitonic_sort.c @@ -13,8 +13,6 @@ * 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. */ -#include "swap.h" - /** * Verificar si x es de 2^n * @param x El valor a verificar @@ -55,9 +53,12 @@ int greatest_power_of_two_less_than(int n) { * @param dir La dirección a ordenar, 1 para ascendentemente o 0 por descendentamente * @param array El array a ordenar */ -void compare(int i, int j, int dir, int *array) { // +void compare(int i, int j, int dir, int *array) { + int temp; if (dir == (array[i] > array[j])){ - swap(&array[i], &array[j]); + temp = array[i]; + array[i] = array[j]; + array[j] = temp; } } diff --git a/src/count_sort.c b/src/count_sort.c index 0135485..6ba0c3f 100644 --- a/src/count_sort.c +++ b/src/count_sort.c @@ -35,6 +35,7 @@ void count_sort(int *array, int n) { fprintf(stderr, "Error: Out of heap space!\n"); exit(5); } + memset(count, 0, sizeof(int) *n); memcpy(temp, array, sizeof(int) * n); for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { diff --git a/src/include/swap.h b/src/include/swap.h deleted file mode 100644 index 7ecce3c..0000000 --- a/src/include/swap.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright 2018 Christopher Cromer - * Copyright 2018 Rodolfo Cuevas - * - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - * - * 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. - * - * 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. - * - * 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. - */ - -#ifndef _SORT_SWAP - #define _SORT_SWAP - void swap(int *xp, int *yp); -#endif diff --git a/src/selection_sort.c b/src/selection_sort.c index 6d18107..06c70c4 100644 --- a/src/selection_sort.c +++ b/src/selection_sort.c @@ -13,8 +13,6 @@ * 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. */ -#include "swap.h" - /** * Usar el algoritmo de ordenamentio por selección * @param array El array a ordenar @@ -23,14 +21,17 @@ void selection_sort(int *array, int n) { int i; int j; + int temp; int min_idx; - for (i = 0; i < n-1; i++) { + for (i = 0; i < n - 1; i++) { min_idx = i; for (j = i + 1; j < n; j++) { if (array[j] < array[min_idx]) { min_idx = j; } } - swap(&array[min_idx], &array[i]); + temp = array[min_idx]; + array[min_idx] = array[i]; + array[i] = temp; } } diff --git a/src/sort.c b/src/sort.c index d232a5c..418430e 100644 --- a/src/sort.c +++ b/src/sort.c @@ -346,7 +346,7 @@ int main (int argc, char **argv) { end_sort(); } - // O(n+k) + // O(n^2) if (count) { start_sort("Count sort corriendo... ", n); count_sort(work_array, n); @@ -354,16 +354,16 @@ int main (int argc, char **argv) { } // O(n^2) - if (bubble) { - start_sort("Bubble sort corriendo... ", n); - bubble_sort(work_array, n); + if (selection) { + start_sort("Selection sort corriendo... ", n); + selection_sort(work_array, n); end_sort(); } // O(n^2) - if (selection) { - start_sort("Selection sort corriendo... ", n); - selection_sort(work_array, n); + if (bubble) { + start_sort("Bubble sort corriendo... ", n); + bubble_sort(work_array, n); end_sort(); } diff --git a/src/swap.c b/src/swap.c deleted file mode 100644 index 517cee2..0000000 --- a/src/swap.c +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2018 Christopher Cromer - * Copyright 2018 Rodolfo Cuevas - * - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - * - * 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. - * - * 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. - * - * 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. - */ - -/** - * Intercambiar dos valores - * @param xp Primer valor - * @param yp Segundo valor - */ -void swap(int *xp, int *yp) { - if (xp == yp) { - return; - } - *xp = *xp + *yp; - *yp = *xp - *yp; - *xp = *xp - *yp; -} diff --git a/src/timer.c b/src/timer.c index 9783992..4da155f 100644 --- a/src/timer.c +++ b/src/timer.c @@ -15,33 +15,34 @@ #include #include +#include /** * Cuando empezó el timer */ -static time_t start_time = 0; +static struct timespec start_time; /** * Cuando terminó el timer */ -static time_t stop_time = 0; +static struct timespec stop_time; /** * Empezar el timer */ void start_timer() { - time(&start_time); - stop_time = 0; + clock_gettime(CLOCK_MONOTONIC, &start_time); + stop_time.tv_nsec = 0; } /** * Terminar el timer */ void stop_timer() { - if (start_time == 0) { + if (start_time.tv_nsec == 0) { fprintf(stderr, "Error: El temporizador no ha comenzado todavía!\n"); } else { - time(&stop_time); + clock_gettime(CLOCK_MONOTONIC, &stop_time); } } @@ -49,16 +50,21 @@ void stop_timer() { * Imprimir el tiempo de ejecución */ void print_timer() { - if (start_time == 0) { + if (start_time.tv_nsec == 0) { fprintf(stderr, "Error: El temporizador no ha comenzado todavía!\n"); } - else if (stop_time == 0) { + else if (stop_time.tv_nsec == 0) { fprintf(stderr, "Error: El temporizador no ha terminado todavía!\n"); } else { - long int seconds = (long int) (stop_time - start_time); - long int minutes = 0; - long int hours = 0; + time_t seconds = stop_time.tv_sec - start_time.tv_sec; + unsigned long long milliseconds = round((stop_time.tv_nsec - start_time.tv_nsec)/ 1.0e6); // Convert nanoseconds to milliseconds + if (milliseconds > 999) { + seconds++; + milliseconds = 0; + } + unsigned int minutes = 0; + unsigned int hours = 0; if (seconds >= 3600) { hours = seconds / 3600; seconds = seconds - (hours * 3600); @@ -66,29 +72,15 @@ void print_timer() { minutes = seconds / 60; seconds = seconds - (minutes * 60); } - fprintf(stdout, "Tiempo de ejecución: %ld horas, %ld minutos y %ld segundos\n", hours, minutes, seconds); + fprintf(stdout, "Tiempo de ejecución: %d horas, %d minutos y %ld.%lld segundos\n", hours, minutes, seconds, milliseconds); } else if (seconds >= 60) { minutes = seconds / 60; seconds = seconds - (minutes * 60); - fprintf(stdout, "Tiempo de ejecución: %ld minutos y %ld segundos\n", minutes, seconds); + fprintf(stdout, "Tiempo de ejecución: %d minutos y %ld.%lld segundos\n", minutes, seconds, milliseconds); } else { - fprintf(stdout, "Tiempo de ejecución: %ld segundos\n", seconds); + fprintf(stdout, "Tiempo de ejecución: %ld.%lld segundos\n", seconds, milliseconds); } } } - -/** - * Devolver el tiempo que demoró en ejecutarse - * @return El tiempo que demoró - */ -long int get_timer() { - 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"); - } - return (long int) (stop_time - start_time); -} diff --git a/test/Makefile b/test/Makefile index d49a69c..fc1b030 100644 --- a/test/Makefile +++ b/test/Makefile @@ -3,7 +3,7 @@ CFLAGS=-Wall -I../src/include -DDEBUG -g SRC=test.c OBJ=$(SRC:.c=.o) -OBJ+=../src/random.o ../src/swap.o ../src/bubble_sort.o ../src/timer.o ../src/count_sort.o ../src/quick_sort.o ../src/merge_sort.o ../src/bitonic_sort.o ../src/selection_sort.o +OBJ+=../src/random.o ../src/bubble_sort.o ../src/count_sort.o ../src/quick_sort.o ../src/merge_sort.o ../src/bitonic_sort.o ../src/selection_sort.o all: test