agregar bitonicsort for a power of 2

This commit is contained in:
Rodolfo Cuevas 2018-11-16 14:37:39 -03:00
parent 67aab5f842
commit a91f3e2b59
5 changed files with 85 additions and 3 deletions

View File

@ -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

59
src/bitonic_sort.c Normal file
View File

@ -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 <bitonic_sort.h>
#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);
}

View File

@ -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

View File

@ -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

View File

@ -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;