cleanup files

This commit is contained in:
Chris Cromer 2018-11-17 13:35:31 -03:00
parent 429ef47a78
commit 18fc551db3
Signed by: cromer
GPG Key ID: 39CC813FF3C8708A
8 changed files with 149 additions and 132 deletions

View File

@ -1,7 +1,7 @@
CC=gcc CC=gcc
CFLAGS=-Wall -Isrc/include -DDEBUG -g CFLAGS=-Wall -Isrc/include -DDEBUG -g
#LDFLAGS=-lm #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/merge_sort.c src/bitonic_sort.c src/selection_sort.c SRC=src/sort.c src/random.c src/swap.c src/bubble_sort.c src/timer.c src/count_sort.c src/quick_sort.c src/merge_sort.c src/bitonic_sort.c src/selection_sort.c
OBJ=$(SRC:.c=.o) OBJ=$(SRC:.c=.o)
all: sort informe all: sort informe

View File

@ -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. * 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> #include "swap.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); * 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])){ if (c > 1){
SWAP(array[i], array[j]); 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; * 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;
if (c > 1){ if (c > 1){
k = c / 2; k = c / 2;
for (i = low;i < low+k ;i++){ recbitonic(low, k, 1, array);
compare(i, i+k, dir, array); recbitonic(low + k, k, 0, array);
} bitonicmerge(low, c, 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; * Ordenar el arreglo completo
* @param array El array a ordenar
if (c > 1){ * @param n El tamaño del array
k = c / 2; * @param dir La dirección a ordenar, 1 para ascendentemente o 0 por descendentamente
recbitonic(low, k, up, array); */
recbitonic(low + k, k, down, array); void sort(int *array, int n, int dir) {
bitonicmerge(low, c, dir, array); 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);
}

View File

@ -16,8 +16,4 @@
#ifndef _SORT_BITONIC #ifndef _SORT_BITONIC
#define _SORT_BITONIC #define _SORT_BITONIC
void bitonic_sort(int *array, int n); void bitonic_sort(int *array, int n);
void compare(int i, int j, int dir, int *array); #endif
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

@ -16,6 +16,4 @@
#ifndef _SORT_MERGE #ifndef _SORT_MERGE
#define _SORT_MERGE #define _SORT_MERGE
void merge_sort(int *array, int n); 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 #endif

View File

@ -13,35 +13,8 @@
* 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. * 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 <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#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 temp Un array temporario para trabajar
* @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 * Unir los resultados de "dividir y conquistar" de merge sort
@ -78,3 +51,34 @@ void merge(int *array, int *temp, int prev_left, int prev_middle, int right) {
array[left + i] = temp[i]; array[left + i] = temp[i];
} }
} }
/**
* Correr 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
*/
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);
}
}
/**
* 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);
if (temp == NULL) {
fprintf(stderr, "Error: Out of heap space!\n");
exit(5);
}
merge_sort_run(array, temp, 0, n - 1);
free(temp);
}

View File

@ -12,24 +12,25 @@
* *
* 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. * 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.
*/ */
void swap(int *xp, int *yp){
int temp = *xp;
*xp = *yp;
*yp = temp;
}
#include "swap.h"
/**
* Usar el algoritmo de ordenamentio por selección
* @param array El array a ordenar
* @param n El tamaño del array
*/
void selection_sort(int *array, int n) { void selection_sort(int *array, int n) {
int i, j, min_idx; int i;
int j;
int min_idx;
for (i = 0; i < n-1; i++) for (i = 0; i < n-1; i++) {
{ min_idx = i;
min_idx = i; for (j = i + 1; j < n; j++) {
for (j = i+1; j < n; j++) if (array[j] < array[min_idx]) {
if (array[j] < array[min_idx]) min_idx = j;
min_idx = j; }
}
swap(&array[min_idx], &array[i]); swap(&array[min_idx], &array[i]);
} }
} }

View File

@ -138,6 +138,26 @@ void print_invalid_n() {
exit(7); exit(7);
} }
/**
* Empezar los pasos antes de ordenar
*/
void start_sort(const char *message, int n) {
fprintf(stdout, "%s", message);
fflush(stdout);
memcpy(work_array, unordered_array, sizeof(int) * n);
start_timer();
}
/**
* Empezar los pasos después de ordenar
*/
void end_sort() {
stop_timer();
fprintf(stdout, "done\n");
print_timer();
fprintf(stdout, "\n");
}
/** /**
* Liberar la memoria al salir * Liberar la memoria al salir
*/ */
@ -306,75 +326,45 @@ int main (int argc, char **argv) {
} }
if (merge) { if (merge) {
fprintf(stdout, "Merge sort corriendo... "); start_sort("Merge sort corriendo... ", n);
fflush(stdout);
memcpy(work_array, unordered_array, sizeof(int) * n);
start_timer();
merge_sort(work_array, n); merge_sort(work_array, n);
stop_timer(); end_sort();
fprintf(stdout, "done\n");
print_timer();
} }
if (quick) { if (quick) {
fprintf(stdout, "Quick sort corriendo... "); start_sort("Quick sort corriendo... ", n);
fflush(stdout);
memcpy(work_array, unordered_array, sizeof(int) * n);
start_timer();
quick_sort(work_array, n); quick_sort(work_array, n);
stop_timer(); end_sort();
fprintf(stdout, "done\n");
print_timer();
} }
if (bubble) { if (bubble) {
fprintf(stdout, "Bubble sort corriendo... "); start_sort("Bubble sort corriendo... ", n);
fflush(stdout);
memcpy(work_array, unordered_array, sizeof(int) * n);
start_timer();
bubble_sort(work_array, n); bubble_sort(work_array, n);
stop_timer(); end_sort();
fprintf(stdout, "done\n");
print_timer();
} }
if (bitonic) { if (bitonic) {
fprintf(stdout, "Bitonic sort corriendo... "); start_sort("Bitonic sort corriendo... ", n);
fflush(stdout);
memcpy(work_array, unordered_array, sizeof(int) * n);
start_timer();
bitonic_sort(work_array, n); bitonic_sort(work_array, n);
stop_timer(); end_sort();
fprintf(stdout, "done\n");
print_timer();
} }
if (count) { if (count) {
fprintf(stdout, "Count sort corriendo... "); start_sort("Count sort corriendo... ", n);
fflush(stdout);
memcpy(work_array, unordered_array, sizeof(int) * n);
start_timer();
count_sort(work_array, n); count_sort(work_array, n);
stop_timer(); end_sort();
fprintf(stdout, "done\n");
print_timer();
} }
if (selection) { if (selection) {
fprintf(stdout, "Selection sort corriendo... "); start_sort("Selection sort corriendo... ", n);
fflush(stdout);
memcpy(work_array, unordered_array, sizeof(int) * n);
start_timer();
selection_sort(work_array, n); selection_sort(work_array, n);
stop_timer(); end_sort();
fprintf(stdout, "done\n");
print_timer();
} }
if (imprimir) { if (imprimir) {
fprintf(stdout, "\nAntes:\n"); fprintf(stdout, "Antes:\n");
print_array(unordered_array, n); print_array(unordered_array, n);
fprintf(stdout, "\nDespués:\n"); fprintf(stdout, "Después:\n");
print_array(work_array, n); print_array(work_array, n);
} }
return 0; return 0;

View File

@ -3,7 +3,7 @@ CFLAGS=-Wall -I../src/include -DDEBUG -g
SRC=test.c SRC=test.c
OBJ=$(SRC:.c=.o) OBJ=$(SRC:.c=.o)
OBJ+=../src/random.o ../src/bubble_sort.o ../src/timer.o ../src/count_sort.o ../src/quick_sort.o ../src/merge_sort.o ../src/bitonic_sort.o ../src/selection_sort.o OBJ+=../src/random.o ../src/swap.o ../src/bubble_sort.o ../src/timer.o ../src/count_sort.o ../src/quick_sort.o ../src/merge_sort.o ../src/bitonic_sort.o ../src/selection_sort.o
all: test all: test