Browse Source

Merge branch 'cleanup' of UBB/sort into master

tags/1.0.0
Chris Cromer 11 months ago
parent
commit
ca5946b92a
11 changed files with 220 additions and 135 deletions
  1. 1
    1
      Makefile
  2. 62
    34
      src/bitonic_sort.c
  3. 1
    5
      src/include/bitonic_sort.h
  4. 0
    2
      src/include/merge_sort.h
  5. 19
    0
      src/include/swap.h
  6. 32
    28
      src/merge_sort.c
  7. 19
    18
      src/selection_sort.c
  8. 34
    44
      src/sort.c
  9. 25
    0
      src/swap.c
  10. 26
    2
      src/timer.c
  11. 1
    1
      test/Makefile

+ 1
- 1
Makefile 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

+ 62
- 34
src/bitonic_sort.c 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;
#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);
}

+ 1
- 5
src/include/bitonic_sort.h 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
#endif

+ 0
- 2
src/include/merge_sort.h 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
- 0
src/include/swap.h 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

+ 32
- 28
src/merge_sort.c 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);
}

+ 19
- 18
src/selection_sort.c 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++)
{
min_idx = i;
for (j = i+1; j < n; j++)
if (array[j] < array[min_idx])
min_idx = j;
swap(&array[min_idx], &array[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]) {
min_idx = j;
}
}
swap(&array[min_idx], &array[i]);
}
}


+ 34
- 44
src/sort.c 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
- 0
src/swap.c 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;
}

+ 26
- 2
src/timer.c 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);
}

+ 1
- 1
test/Makefile 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


Loading…
Cancel
Save