Merge branch 'cleanup' of UBB/sort into master
This commit is contained in:
commit
ca5946b92a
2
Makefile
2
Makefile
@ -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
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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);
|
|
||||||
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
|
@ -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
|
||||||
|
19
src/include/swap.h
Normal file
19
src/include/swap.h
Normal 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
|
@ -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);
|
||||||
|
}
|
||||||
|
@ -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]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
78
src/sort.c
78
src/sort.c
@ -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;
|
||||||
|
25
src/swap.c
Normal file
25
src/swap.c
Normal 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;
|
||||||
|
}
|
28
src/timer.c
28
src/timer.c
@ -15,7 +15,6 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include "timer.h"
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cuando empezó el timer
|
* Cuando empezó el timer
|
||||||
@ -57,7 +56,26 @@ void print_timer() {
|
|||||||
fprintf(stderr, "Error: El temporizador no ha terminado todavía!\n");
|
fprintf(stderr, "Error: El temporizador no ha terminado todavía!\n");
|
||||||
}
|
}
|
||||||
else {
|
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ó
|
* @return El tiempo que demoró
|
||||||
*/
|
*/
|
||||||
long int get_timer() {
|
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);
|
return (long int) (stop_time - start_time);
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user