Merge branch 'cleanup' of UBB/sort into master

This commit is contained in:
Chris Cromer 2018-11-17 13:36:44 -03:00 committed by Gitea
commit ca5946b92a
11 changed files with 219 additions and 134 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/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)
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.
*/
#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;
#include "swap.h"
/**
* 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]);
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
/**
* 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 (c > 1){
k = c / 2;
recbitonic(low, k, up, array);
recbitonic(low + k, k, down, array);
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;
if (c > 1){
k = c / 2;
recbitonic(low, k, 1, array);
recbitonic(low + k, k, 0, array);
bitonicmerge(low, c, dir, array);
}
}
void sort(int *array, int n){ //ordena el arreglo completo
recbitonic(0, n, up, 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);
}
/**
* 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
#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

@ -16,6 +16,4 @@
#ifndef _SORT_MERGE
#define _SORT_MERGE
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

19
src/include/swap.h Normal file
View File

@ -0,0 +1,19 @@
/*
* 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_SWAP
#define _SORT_SWAP
void swap(int *xp, int *yp);
#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.
*/
#include <stdio.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
@ -78,3 +51,34 @@ void merge(int *array, int *temp, int prev_left, int prev_middle, int right) {
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.
*/
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) {
int i, j, min_idx;
for (i = 0; i < n-1; i++)
{
int i;
int j;
int min_idx;
for (i = 0; i < n-1; i++) {
min_idx = i;
for (j = i+1; j < n; j++)
if (array[j] < array[min_idx])
for (j = i + 1; j < n; j++) {
if (array[j] < array[min_idx]) {
min_idx = j;
}
}
swap(&array[min_idx], &array[i]);
}
}

View File

@ -138,6 +138,26 @@ void print_invalid_n() {
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
*/
@ -306,75 +326,45 @@ int main (int argc, char **argv) {
}
if (merge) {
fprintf(stdout, "Merge sort corriendo... ");
fflush(stdout);
memcpy(work_array, unordered_array, sizeof(int) * n);
start_timer();
start_sort("Merge sort corriendo... ", n);
merge_sort(work_array, n);
stop_timer();
fprintf(stdout, "done\n");
print_timer();
end_sort();
}
if (quick) {
fprintf(stdout, "Quick sort corriendo... ");
fflush(stdout);
memcpy(work_array, unordered_array, sizeof(int) * n);
start_timer();
start_sort("Quick sort corriendo... ", n);
quick_sort(work_array, n);
stop_timer();
fprintf(stdout, "done\n");
print_timer();
end_sort();
}
if (bubble) {
fprintf(stdout, "Bubble sort corriendo... ");
fflush(stdout);
memcpy(work_array, unordered_array, sizeof(int) * n);
start_timer();
start_sort("Bubble sort corriendo... ", n);
bubble_sort(work_array, n);
stop_timer();
fprintf(stdout, "done\n");
print_timer();
end_sort();
}
if (bitonic) {
fprintf(stdout, "Bitonic sort corriendo... ");
fflush(stdout);
memcpy(work_array, unordered_array, sizeof(int) * n);
start_timer();
start_sort("Bitonic sort corriendo... ", n);
bitonic_sort(work_array, n);
stop_timer();
fprintf(stdout, "done\n");
print_timer();
end_sort();
}
if (count) {
fprintf(stdout, "Count sort corriendo... ");
fflush(stdout);
memcpy(work_array, unordered_array, sizeof(int) * n);
start_timer();
start_sort("Count sort corriendo... ", n);
count_sort(work_array, n);
stop_timer();
fprintf(stdout, "done\n");
print_timer();
end_sort();
}
if (selection) {
fprintf(stdout, "Selection sort corriendo... ");
fflush(stdout);
memcpy(work_array, unordered_array, sizeof(int) * n);
start_timer();
start_sort("Selection sort corriendo... ", n);
selection_sort(work_array, n);
stop_timer();
fprintf(stdout, "done\n");
print_timer();
end_sort();
}
if (imprimir) {
fprintf(stdout, "\nAntes:\n");
fprintf(stdout, "Antes:\n");
print_array(unordered_array, n);
fprintf(stdout, "\nDespués:\n");
fprintf(stdout, "Después:\n");
print_array(work_array, n);
}
return 0;

25
src/swap.c Normal file
View File

@ -0,0 +1,25 @@
/*
* 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.
*/
/**
* Intercambiar dos valores
* @param xp Primer valor
* @param yp Segundo valor
*/
void swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
}

View File

@ -15,7 +15,6 @@
#include <stdio.h>
#include <time.h>
#include "timer.h"
/**
* Cuando empezó el timer
@ -57,7 +56,26 @@ void print_timer() {
fprintf(stderr, "Error: El temporizador no ha terminado todavía!\n");
}
else {
fprintf(stdout, "Tiempo de ejecución: %ld segundos\n", (long int) (stop_time - start_time));
long int seconds = (long int) (stop_time - start_time);
long int minutes = 0;
long int hours = 0;
if (seconds >= 3600) {
hours = seconds / 3600;
seconds = seconds - (hours * 3600);
if (seconds >= 60) {
minutes = seconds / 60;
seconds = seconds - (minutes * 60);
}
fprintf(stdout, "Tiempo de ejecución: %ld horas, %ld minutos y %ld segundos\n", hours, minutes, seconds);
}
else if (seconds >= 60) {
minutes = seconds / 60;
seconds = seconds - (minutes * 60);
fprintf(stdout, "Tiempo de ejecución: %ld minutos y %ld segundos\n", minutes, seconds);
}
else {
fprintf(stdout, "Tiempo de ejecución: %ld segundos\n", seconds);
}
}
}
@ -66,5 +84,11 @@ void print_timer() {
* @return El tiempo que demoró
*/
long int get_timer() {
if (start_time == 0) {
fprintf(stderr, "Error: El temporizador no ha comenzado todavía!\n");
}
else if (stop_time == 0) {
fprintf(stderr, "Error: El temporizador no ha terminado todavía!\n");
}
return (long int) (stop_time - start_time);
}

View File

@ -3,7 +3,7 @@ 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 ../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