From 0fa4c08a72e7bf6f8a7486cdc05a5feec460f422 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 14 Nov 2018 17:53:45 -0300 Subject: [PATCH 1/2] implement merge sort --- Makefile | 2 +- src/include/merge_sort.h | 21 ++++++++++++ src/merge_sort.c | 74 ++++++++++++++++++++++++++++++++++++++++ src/sort.c | 10 +++++- test/Makefile | 2 +- test/test.c | 20 +++++++++++ 6 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 src/include/merge_sort.h create mode 100644 src/merge_sort.c diff --git a/Makefile b/Makefile index 32a33cf..82f9228 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=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 OBJ=$(SRC:.c=.o) all: sort informe diff --git a/src/include/merge_sort.h b/src/include/merge_sort.h new file mode 100644 index 0000000..1701ff0 --- /dev/null +++ b/src/include/merge_sort.h @@ -0,0 +1,21 @@ +/* + * 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_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 new file mode 100644 index 0000000..fd91065 --- /dev/null +++ b/src/merge_sort.c @@ -0,0 +1,74 @@ +/* + * 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. + */ + +#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 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 + */ +void merge(int *array, int *temp, int prev_left, int prev_middle, int right) { + int i = 0; + int left = prev_left; + int middle = prev_middle - 1; + int far_right = right - left + 1; + + while (prev_left <= middle && prev_middle <= right) { + if (array[prev_left] < array[prev_middle]) { + temp[i++] = array[prev_left++]; + } + else { + temp[i++] = array[prev_middle++]; + } + } + + while (prev_left <= middle) { + temp[i++] = array[prev_left++]; + } + + while (prev_middle <= right) { + temp[i++] = array[prev_middle++]; + } + + for (i = 0; i < far_right; i++) { + array[left + i] = temp[i]; + } +} diff --git a/src/sort.c b/src/sort.c index 59b6c42..e979a72 100644 --- a/src/sort.c +++ b/src/sort.c @@ -22,6 +22,7 @@ #include "bubble_sort.h" #include "count_sort.h" #include "quick_sort.h" +#include "merge_sort.h" #define SORT_VERSION "1.0.0" @@ -217,7 +218,14 @@ int main (int argc, char **argv) { } if (merge) { - // merge sort + fprintf(stdout, "Merge sort corriendo... "); + fflush(stdout); + memcpy(work_array, unordered_array, sizeof(int) * n); + start_timer(); + merge_sort(work_array, n); + stop_timer(); + fprintf(stdout, "done\n"); + print_timer(); } if (quick) { diff --git a/test/Makefile b/test/Makefile index 55c22d0..b07c0b2 100644 --- a/test/Makefile +++ b/test/Makefile @@ -2,7 +2,7 @@ CC=gcc 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 +OBJ+=../src/random.o ../src/bubble_sort.o ../src/timer.o ../src/count_sort.o ../src/quick_sort.o ../src/merge_sort.o all: test diff --git a/test/test.c b/test/test.c index 048c9f5..1e9142b 100644 --- a/test/test.c +++ b/test/test.c @@ -21,6 +21,7 @@ #include "bubble_sort.h" #include "count_sort.h" #include "quick_sort.h" +#include "merge_sort.h" static int *test_case; static int *test_array; @@ -146,6 +147,25 @@ int main(int argc, char **argv) { passed++; } + // Test merge sort + pass = 1; + memcpy(test_array, test_case, sizeof(int) * n); + fprintf(stdout, "\tmerge sort: "); + fflush(stdout); + merge_sort(test_array, n); + for (i = 0; i < n; i++) { + if (test_array[i] != qarray[i]) { + fprintf(stdout, "fail\n"); + failed++; + pass = 0; + break; + } + } + if (pass) { + fprintf(stdout, "pass\n"); + passed++; + } + fprintf(stdout, "%d tests passed\n", passed); fprintf(stdout, "%d tests failed\n", failed); From 0875b5ffbaea0f01a7934cb320f364aeb72b713a Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 14 Nov 2018 17:57:01 -0300 Subject: [PATCH 2/2] add missing doc --- src/merge_sort.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/merge_sort.c b/src/merge_sort.c index fd91065..e4ddbbb 100644 --- a/src/merge_sort.c +++ b/src/merge_sort.c @@ -30,6 +30,7 @@ void merge_sort(int *array, int n) { /** * 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 */ @@ -44,6 +45,11 @@ void merge_sort_run(int *array, int *temp, int left, int right) { /** * Unir los resultados de "dividir y conquistar" de merge sort + * @param array El array a ordenar + * @param temp Un array temporario para trabajar + * @param prev_left El lado izquerda anterior + * @param prev_middle La particion de medio anterior + * @param right El lado derecha */ void merge(int *array, int *temp, int prev_left, int prev_middle, int right) { int i = 0;