From 18fc551db39394391edaf50cfefdd1dd90541f84 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 17 Nov 2018 13:35:31 -0300 Subject: [PATCH] cleanup files --- Makefile | 2 +- src/bitonic_sort.c | 94 +++++++++++++++++++++++++------------- src/include/bitonic_sort.h | 6 +-- src/include/merge_sort.h | 2 - src/merge_sort.c | 60 ++++++++++++------------ src/selection_sort.c | 37 +++++++-------- src/sort.c | 78 ++++++++++++++----------------- test/Makefile | 2 +- 8 files changed, 149 insertions(+), 132 deletions(-) diff --git a/Makefile b/Makefile index bdfb1ba..9cd7669 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/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 +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 OBJ=$(SRC:.c=.o) all: sort informe diff --git a/src/bitonic_sort.c b/src/bitonic_sort.c index b1d9390..0875231 100644 --- a/src/bitonic_sort.c +++ b/src/bitonic_sort.c @@ -13,47 +13,75 @@ * 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 -#define SWAP(x,y) t = x; x = y; y = t; //definicion del cambio de SWAP que utiliza compare - int up = 1; - int down = 0; +#include "swap.h" -void bitonic_sort(int *array, int n){ //funcion bitonic - sort(array, n); +/** + * Comparar y intercambiar los valores para darle orden + * @param i El primer indice a comparar + * @param j El segundo indice a comparar + * @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) { // + if (dir == (array[i] > array[j])){ + swap(&array[i], &array[j]); + } } -void compare(int i, int j, int dir, int *array){ //compara y cambia los valores para darle orden - int t; +/** + * Unir la secuencia + * @param low El parte inferior + * @param c El parte superior + * @param dir La dirección a ordenar, 1 para ascendentemente o 0 por descendentamente + * @param array El array a ordenar + */ +void bitonicmerge(int low, int c, int dir, int *array) { + int i; + int k; - if (dir == (array[i] > array[j])){ - SWAP(array[i], array[j]); - } + if (c > 1){ + k = c / 2; + for (i = low; i < low + k; i++){ + compare(i, i + k, dir, array); + } + bitonicmerge(low, k, dir, array); + bitonicmerge(low + k, k, dir, array); + } } -void bitonicmerge(int low, int c, int dir, int *array){ //ordena la secuenca ascendentemente si dir=1 - int k, i; +/** + * Generar la secuencia bitonica en forma de piramide + * @param low El parte inferior + * @param c El parte superior + * @param dir La dirección a ordenar, 1 para ascendentemente o 0 por descendentamente + * @param array El array a ordenar + */ +void recbitonic(int low, int c, int dir, int *array) { + int k; - if (c > 1){ - k = c / 2; - for (i = low;i < low+k ;i++){ - compare(i, i+k, dir, array); - } - bitonicmerge(low, k, dir, array); - bitonicmerge(low+k, k, dir, array); - } + if (c > 1){ + k = c / 2; + recbitonic(low, k, 1, array); + recbitonic(low + k, k, 0, array); + bitonicmerge(low, c, dir, array); + } } -void recbitonic(int low, int c, int dir, int *array){ //genera la secuencia bitonica en forma de piramide - int k; - - if (c > 1){ - k = c / 2; - recbitonic(low, k, up, array); - recbitonic(low + k, k, down, array); - bitonicmerge(low, c, dir, array); - } +/** + * Ordenar el arreglo completo + * @param array El array a ordenar + * @param n El tamaño del array + * @param dir La dirección a ordenar, 1 para ascendentemente o 0 por descendentamente + */ +void sort(int *array, int n, int dir) { + recbitonic(0, n, dir, array); } -void sort(int *array, int n){ //ordena el arreglo completo - recbitonic(0, n, up, array); -} +/** + * Usar el algoritmo de bitonic sort + * @param array El array a ordenar + * @param n El tamaño del array + */ +void bitonic_sort(int *array, int n) { + sort(array, n, 1); +} diff --git a/src/include/bitonic_sort.h b/src/include/bitonic_sort.h index 9cda0b8..7cbd159 100644 --- a/src/include/bitonic_sort.h +++ b/src/include/bitonic_sort.h @@ -16,8 +16,4 @@ #ifndef _SORT_BITONIC #define _SORT_BITONIC void bitonic_sort(int *array, int n); - void compare(int i, int j, int dir, int *array); - void bitonicmerge(int low, int c, int dir, int *array); - void recbitonic(int low, int c, int dir, int *array); - void sort(int *array, int n); -#endif \ No newline at end of file +#endif diff --git a/src/include/merge_sort.h b/src/include/merge_sort.h index 1701ff0..02d4eeb 100644 --- a/src/include/merge_sort.h +++ b/src/include/merge_sort.h @@ -16,6 +16,4 @@ #ifndef _SORT_MERGE #define _SORT_MERGE void merge_sort(int *array, int n); - void merge_sort_run(int *array, int *temp, int left, int right); - void merge(int *array, int *temp, int prev_left, int prev_middle, int right); #endif diff --git a/src/merge_sort.c b/src/merge_sort.c index e4ddbbb..69c2460 100644 --- a/src/merge_sort.c +++ b/src/merge_sort.c @@ -13,35 +13,8 @@ * 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 #include -#include "merge_sort.h" - -/** - * Usar el algoritmo de merge sort - * @param array El array a ordenar - * @param n El tamaño del array - */ -void merge_sort(int *array, int n) { - int *temp = malloc(sizeof(int)* n); - merge_sort_run(array, temp, 0, n - 1); - free(temp); -} - -/** - * Correro el merge sort recursivamente - * @param array El array a ordenar - * @param temp Un array temporario para trabajar - * @param left El lado izquerda a ordenar - * @param right El lado derercha a ordenar - */ -void merge_sort_run(int *array, int *temp, int left, int right) { - if (left != right) { - int middle = (left + right) / 2; - merge_sort_run(array, temp, left, middle); - merge_sort_run(array, temp, middle + 1, right); - merge(array, temp, left, middle + 1, right); - } -} /** * Unir los resultados de "dividir y conquistar" de merge sort @@ -78,3 +51,34 @@ void merge(int *array, int *temp, int prev_left, int prev_middle, int right) { array[left + i] = temp[i]; } } + +/** + * Correr el merge sort recursivamente + * @param array El array a ordenar + * @param temp Un array temporario para trabajar + * @param left El lado izquerda a ordenar + * @param right El lado derercha a ordenar + */ +void merge_sort_run(int *array, int *temp, int left, int right) { + if (left != right) { + int middle = (left + right) / 2; + merge_sort_run(array, temp, left, middle); + merge_sort_run(array, temp, middle + 1, right); + merge(array, temp, left, middle + 1, right); + } +} + +/** + * Usar el algoritmo de merge sort + * @param array El array a ordenar + * @param n El tamaño del array + */ +void merge_sort(int *array, int n) { + int *temp = malloc(sizeof(int)* n); + if (temp == NULL) { + fprintf(stderr, "Error: Out of heap space!\n"); + exit(5); + } + merge_sort_run(array, temp, 0, n - 1); + free(temp); +} diff --git a/src/selection_sort.c b/src/selection_sort.c index 72ffffe..6d18107 100644 --- a/src/selection_sort.c +++ b/src/selection_sort.c @@ -12,24 +12,25 @@ * * 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. */ -void swap(int *xp, int *yp){ - int temp = *xp; - *xp = *yp; - *yp = temp; -} +#include "swap.h" + +/** + * Usar el algoritmo de ordenamentio por selección + * @param array El array a ordenar + * @param n El tamaño del array + */ void selection_sort(int *array, int n) { - int i, j, min_idx; - - - 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]); - } + int i; + int j; + int min_idx; + 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]); + } } - diff --git a/src/sort.c b/src/sort.c index 8e49ddb..956e87b 100644 --- a/src/sort.c +++ b/src/sort.c @@ -138,6 +138,26 @@ void print_invalid_n() { exit(7); } +/** + * Empezar los pasos antes de ordenar + */ +void start_sort(const char *message, int n) { + fprintf(stdout, "%s", message); + fflush(stdout); + memcpy(work_array, unordered_array, sizeof(int) * n); + start_timer(); +} + +/** + * Empezar los pasos después de ordenar + */ +void end_sort() { + stop_timer(); + fprintf(stdout, "done\n"); + print_timer(); + fprintf(stdout, "\n"); +} + /** * Liberar la memoria al salir */ @@ -306,75 +326,45 @@ int main (int argc, char **argv) { } if (merge) { - fprintf(stdout, "Merge sort corriendo... "); - fflush(stdout); - memcpy(work_array, unordered_array, sizeof(int) * n); - start_timer(); + start_sort("Merge sort corriendo... ", n); merge_sort(work_array, n); - stop_timer(); - fprintf(stdout, "done\n"); - print_timer(); + end_sort(); } if (quick) { - fprintf(stdout, "Quick sort corriendo... "); - fflush(stdout); - memcpy(work_array, unordered_array, sizeof(int) * n); - start_timer(); + start_sort("Quick sort corriendo... ", n); quick_sort(work_array, n); - stop_timer(); - fprintf(stdout, "done\n"); - print_timer(); + end_sort(); } if (bubble) { - fprintf(stdout, "Bubble sort corriendo... "); - fflush(stdout); - memcpy(work_array, unordered_array, sizeof(int) * n); - start_timer(); + start_sort("Bubble sort corriendo... ", n); bubble_sort(work_array, n); - stop_timer(); - fprintf(stdout, "done\n"); - print_timer(); + end_sort(); } if (bitonic) { - fprintf(stdout, "Bitonic sort corriendo... "); - fflush(stdout); - memcpy(work_array, unordered_array, sizeof(int) * n); - start_timer(); + start_sort("Bitonic sort corriendo... ", n); bitonic_sort(work_array, n); - stop_timer(); - fprintf(stdout, "done\n"); - print_timer(); + end_sort(); } if (count) { - fprintf(stdout, "Count sort corriendo... "); - fflush(stdout); - memcpy(work_array, unordered_array, sizeof(int) * n); - start_timer(); + start_sort("Count sort corriendo... ", n); count_sort(work_array, n); - stop_timer(); - fprintf(stdout, "done\n"); - print_timer(); + end_sort(); } if (selection) { - fprintf(stdout, "Selection sort corriendo... "); - fflush(stdout); - memcpy(work_array, unordered_array, sizeof(int) * n); - start_timer(); + start_sort("Selection sort corriendo... ", n); selection_sort(work_array, n); - stop_timer(); - fprintf(stdout, "done\n"); - print_timer(); + end_sort(); } if (imprimir) { - fprintf(stdout, "\nAntes:\n"); + fprintf(stdout, "Antes:\n"); print_array(unordered_array, n); - fprintf(stdout, "\nDespués:\n"); + fprintf(stdout, "Después:\n"); print_array(work_array, n); } return 0; diff --git a/test/Makefile b/test/Makefile index c99423b..d49a69c 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/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/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 all: test