From a91f3e2b594f28686f5879c030e91cdc9d2477d9 Mon Sep 17 00:00:00 2001 From: Rodolfo Cuevas Date: Fri, 16 Nov 2018 14:37:39 -0300 Subject: [PATCH] agregar bitonicsort for a power of 2 --- Makefile | 2 +- src/bitonic_sort.c | 59 ++++++++++++++++++++++++++++++++++++++ src/include/bitonic_sort.h | 23 +++++++++++++++ test/Makefile | 2 +- test/test.c | 2 +- 5 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 src/bitonic_sort.c create mode 100644 src/include/bitonic_sort.h diff --git a/Makefile b/Makefile index 32a33cf..900babb 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/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 new file mode 100644 index 0000000..b1d9390 --- /dev/null +++ b/src/bitonic_sort.c @@ -0,0 +1,59 @@ +/* + * 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 +#define SWAP(x,y) t = x; x = y; y = t; //definicion del cambio de SWAP que utiliza compare + int up = 1; + int down = 0; + +void bitonic_sort(int *array, int n){ //funcion bitonic + sort(array, n); +} + +void compare(int i, int j, int dir, int *array){ //compara y cambia los valores para darle orden + int t; + + if (dir == (array[i] > array[j])){ + SWAP(array[i], array[j]); + } +} + +void bitonicmerge(int low, int c, int dir, int *array){ //ordena la secuenca ascendentemente si dir=1 + int k, i; + + 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 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); + } +} + +void sort(int *array, int n){ //ordena el arreglo completo + recbitonic(0, n, up, array); +} diff --git a/src/include/bitonic_sort.h b/src/include/bitonic_sort.h new file mode 100644 index 0000000..9cda0b8 --- /dev/null +++ b/src/include/bitonic_sort.h @@ -0,0 +1,23 @@ +/* + * 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_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 diff --git a/test/Makefile b/test/Makefile index 55c22d0..8a6beb5 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/bitonic_sort.o ../src/selection_sort.o all: test diff --git a/test/test.c b/test/test.c index 997b752..3f44655 100644 --- a/test/test.c +++ b/test/test.c @@ -51,7 +51,7 @@ void cleanup() { * El programa de test */ int main(int argc, char **argv) { - int n = 50000; + int n = 65536; int i; int gen; int pass;