sort/src/bitonic_sort.c

60 líneas
2.6 KiB
C

/*
* 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);
}