|
|
|
@ -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 <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; |
|
|
|
|
#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;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
/**
|
|
|
|
|
* 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; |
|
|
|
|
|
|
|
|
|
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, 1, array); |
|
|
|
|
recbitonic(low + k, k, 0, array); |
|
|
|
|
bitonicmerge(low, c, dir, array); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|