merge changes
This commit is contained in:
commit
7baa09423d
2
Makefile
2
Makefile
@ -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/quick_sort.c
|
||||
SRC=src/sort.c src/random.c src/bubble_sort.c src/timer.c src/count_sort.c src/quick_sort.c
|
||||
OBJ=$(SRC:.c=.o)
|
||||
|
||||
all: sort informe
|
||||
|
@ -13,6 +13,11 @@
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Usar el algoritmo de bubble sort
|
||||
* @param array El array a ordenar
|
||||
* @param n El tamaño del array
|
||||
*/
|
||||
void bubble_sort(int *array, int n) {
|
||||
int i;
|
||||
int j = n;
|
||||
|
47
src/count_sort.c
Normal file
47
src/count_sort.c
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* Usar el algoritmo de ordenameniento por conteo
|
||||
* @param array El array a ordenar
|
||||
* @param n El tamaño del array
|
||||
*/
|
||||
void count_sort(int *array, int n) {
|
||||
int i;
|
||||
int j;
|
||||
int *temp = malloc(sizeof(int) * n);
|
||||
int *count = malloc(sizeof(int) * n);
|
||||
memcpy(temp, array, sizeof(int) * n);
|
||||
for (i = 0; i < n - 1; i++) {
|
||||
for (j = i + 1; j < n; j++) {
|
||||
if (temp[i] < temp[j]) {
|
||||
count[j]++;
|
||||
}
|
||||
else {
|
||||
count[i]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
array[count[i]] = temp[i];
|
||||
}
|
||||
|
||||
free(count);
|
||||
free(temp);
|
||||
}
|
19
src/include/count_sort.h
Normal file
19
src/include/count_sort.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_COUNT
|
||||
#define _SORT_COUNT
|
||||
void count_sort(int *array, int n);
|
||||
#endif
|
135
src/sort.c
135
src/sort.c
@ -20,17 +20,23 @@
|
||||
#include "random.h"
|
||||
#include "timer.h"
|
||||
#include "bubble_sort.h"
|
||||
#include "count_sort.h"
|
||||
#include "quick_sort.h"
|
||||
|
||||
#define SORT_VERSION "1.0.0"
|
||||
|
||||
static int *array;
|
||||
/**
|
||||
* El array a ordenar
|
||||
*/
|
||||
static int *unordered_array;
|
||||
static int *work_array;
|
||||
|
||||
/**
|
||||
* Imprimir el uso del programa
|
||||
*/
|
||||
void print_usage() {
|
||||
fprintf(stdout, "uso: sort [OPCIÓN]\n");
|
||||
fprintf(stdout, " -a, --all usar todos los algoritmos de ordenamentio\n");
|
||||
fprintf(stdout, " -m, --merge usar merge sort\n");
|
||||
fprintf(stdout, " -q, --quick usar quick sort\n");
|
||||
fprintf(stdout, " -b, --bubble usar bubble sort\n");
|
||||
@ -46,15 +52,6 @@ void print_usage() {
|
||||
fprintf(stdout, " -v, --version mostrar la versión del programa\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Imprimir un mensaje de error y salir
|
||||
*/
|
||||
void extra_algorithm() {
|
||||
fprintf(stderr, "Error: Solo se puede elegir un algoritmo al vez!\n");
|
||||
print_usage();
|
||||
exit(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Imprimir un array
|
||||
* @param *array El array a imprimir
|
||||
@ -100,7 +97,8 @@ int read_buffer(int *variable) {
|
||||
* Liberar la memoria al salir
|
||||
*/
|
||||
void cleanup() {
|
||||
free(array);
|
||||
free(unordered_array);
|
||||
free(work_array);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -113,10 +111,16 @@ int main (int argc, char **argv) {
|
||||
int n = 10;
|
||||
int elegir = 0;
|
||||
int imprimir = 0;
|
||||
int algoritmo = -1;
|
||||
int merge = 0;
|
||||
int quick = 0;
|
||||
int bubble = 0;
|
||||
int bitonic = 0;
|
||||
int count = 0;
|
||||
int selection = 0;
|
||||
int opt;
|
||||
int long_index = 0;
|
||||
static struct option long_options[] = {
|
||||
{"all", no_argument, 0, 'a'},
|
||||
{"merge", no_argument, 0, 'm'},
|
||||
{"quick", no_argument, 0, 'q'},
|
||||
{"bubble", no_argument, 0, 'b'},
|
||||
@ -135,43 +139,33 @@ int main (int argc, char **argv) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
while ((opt = getopt_long(argc, argv, "mqbBcsn:eiv", long_options, &long_index)) != -1) {
|
||||
while ((opt = getopt_long(argc, argv, "amqbBcsn:eiv", long_options, &long_index)) != -1) {
|
||||
switch (opt) {
|
||||
case 'a':
|
||||
merge = 1;
|
||||
quick = 1;
|
||||
bubble = 1;
|
||||
bitonic = 1;
|
||||
count = 1;
|
||||
selection = 1;
|
||||
break;
|
||||
case 'm':
|
||||
if (algoritmo != -1) {
|
||||
extra_algorithm();
|
||||
}
|
||||
algoritmo = 0;
|
||||
merge = 1;
|
||||
break;
|
||||
case 'q':
|
||||
if (algoritmo != -1) {
|
||||
extra_algorithm();
|
||||
}
|
||||
algoritmo = 1;
|
||||
quick = 1;
|
||||
break;
|
||||
case 'b':
|
||||
if (algoritmo != -1) {
|
||||
extra_algorithm();
|
||||
}
|
||||
algoritmo = 2;
|
||||
bubble = 1;
|
||||
break;
|
||||
case 'B':
|
||||
if (algoritmo != -1) {
|
||||
extra_algorithm();
|
||||
}
|
||||
algoritmo = 3;
|
||||
bitonic = 1;
|
||||
break;
|
||||
case 'c':
|
||||
if (algoritmo != -1) {
|
||||
extra_algorithm();
|
||||
}
|
||||
algoritmo = 4;
|
||||
count = 1;
|
||||
break;
|
||||
case 's':
|
||||
if (algoritmo != -1) {
|
||||
extra_algorithm();
|
||||
}
|
||||
algoritmo = 5;
|
||||
selection = 1;
|
||||
break;
|
||||
case 'n':
|
||||
n = atol(optarg);
|
||||
@ -196,13 +190,14 @@ int main (int argc, char **argv) {
|
||||
}
|
||||
}
|
||||
|
||||
if (algoritmo < 0 || algoritmo > 5) {
|
||||
if (!merge && !quick && !bubble && !bitonic && !count && !selection) {
|
||||
fprintf(stderr, "Error: No se seleccionó un algoritmo valido!\n");
|
||||
print_usage();
|
||||
return 4;
|
||||
}
|
||||
|
||||
array = malloc(sizeof(int) * n);
|
||||
unordered_array = malloc(sizeof(int) * n);
|
||||
work_array = malloc(sizeof(int) * n);
|
||||
atexit(cleanup);
|
||||
|
||||
// Llenar el array con valores para ordenar después
|
||||
@ -214,51 +209,63 @@ int main (int argc, char **argv) {
|
||||
fprintf(stdout, "Número invalido! Tiene que ser mayor de 1!\n");
|
||||
fprintf(stdout, "Elegir elemento %lli: ", i + 1);
|
||||
}
|
||||
array[i] = opt;
|
||||
unordered_array[i] = opt;
|
||||
}
|
||||
else {
|
||||
array[i] = gen_rand((n * 10) * -1, n * 10);
|
||||
unordered_array[i] = gen_rand((n * 10) * -1, n * 10);
|
||||
}
|
||||
}
|
||||
|
||||
if (imprimir) {
|
||||
fprintf(stdout, "Antes:\n");
|
||||
print_array(array, n);
|
||||
}
|
||||
|
||||
if (algoritmo == 0) {
|
||||
if (merge) {
|
||||
// merge sort
|
||||
}
|
||||
else if (algoritmo == 1) {
|
||||
fprintf(stdout, "Quick sort corriendo...\n");
|
||||
|
||||
if (quick) {
|
||||
fprintf(stdout, "Quick sort corriendo... ");
|
||||
fflush(stdout);
|
||||
memcpy(work_array, unordered_array, sizeof(int) * n);
|
||||
start_timer();
|
||||
quick_sort(array, n);
|
||||
stop_timer();
|
||||
}
|
||||
else if (algoritmo == 2) {
|
||||
fprintf(stdout, "Bubble sort corriendo... ");
|
||||
fflush(stdout);
|
||||
start_timer();
|
||||
bubble_sort(array, n);
|
||||
quick_sort(work_array, n);
|
||||
stop_timer();
|
||||
fprintf(stdout, "done\n");
|
||||
print_timer();
|
||||
}
|
||||
else if (algoritmo == 3) {
|
||||
|
||||
if (bubble) {
|
||||
fprintf(stdout, "Bubble sort corriendo... ");
|
||||
fflush(stdout);
|
||||
memcpy(work_array, unordered_array, sizeof(int) * n);
|
||||
start_timer();
|
||||
bubble_sort(work_array, n);
|
||||
stop_timer();
|
||||
fprintf(stdout, "done\n");
|
||||
print_timer();
|
||||
}
|
||||
|
||||
if (bitonic) {
|
||||
// bitonic sort
|
||||
}
|
||||
else if (algoritmo == 4) {
|
||||
// count sort
|
||||
|
||||
if (count) {
|
||||
fprintf(stdout, "Count sort corriendo... ");
|
||||
fflush(stdout);
|
||||
memcpy(work_array, unordered_array, sizeof(int) * n);
|
||||
start_timer();
|
||||
count_sort(work_array, n);
|
||||
stop_timer();
|
||||
fprintf(stdout, "done\n");
|
||||
print_timer();
|
||||
}
|
||||
else if (algoritmo == 5) {
|
||||
|
||||
if (selection) {
|
||||
// selection sort
|
||||
}
|
||||
|
||||
if (imprimir) {
|
||||
fprintf(stdout, "\nAntes:\n");
|
||||
print_array(unordered_array, n);
|
||||
fprintf(stdout, "\nDespués:\n");
|
||||
print_array(array, n);
|
||||
print_array(work_array, n);
|
||||
}
|
||||
|
||||
print_timer();
|
||||
return 0;
|
||||
}
|
||||
|
19
src/timer.c
19
src/timer.c
@ -30,13 +30,8 @@ static time_t stop_time = 0;
|
||||
* Empezar el timer
|
||||
*/
|
||||
void start_timer() {
|
||||
if (start_time != 0) {
|
||||
fprintf(stderr, "Error: El temporizador ya comenzó!\n");
|
||||
}
|
||||
else {
|
||||
time(&start_time);
|
||||
stop_time = 0;
|
||||
}
|
||||
time(&start_time);
|
||||
stop_time = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,7 +50,15 @@ void stop_timer() {
|
||||
* Imprimir el tiempo de ejecución
|
||||
*/
|
||||
void print_timer() {
|
||||
fprintf(stdout, "Tiempo de ejecución: %ld segundos\n", (long int) (stop_time - start_time));
|
||||
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");
|
||||
}
|
||||
else {
|
||||
fprintf(stdout, "Tiempo de ejecución: %ld segundos\n", (long int) (stop_time - start_time));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2,7 +2,7 @@ CC=gcc
|
||||
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/quick_sort.o
|
||||
OBJ+=../src/random.o ../src/bubble_sort.o ../src/timer.o ../src/count_sort.o ../src/quick_sort.o
|
||||
|
||||
all: test
|
||||
|
||||
|
40
test/test.c
40
test/test.c
@ -19,6 +19,7 @@
|
||||
#include <unistd.h>
|
||||
#include "random.h"
|
||||
#include "bubble_sort.h"
|
||||
#include "count_sort.h"
|
||||
#include "quick_sort.h"
|
||||
|
||||
static int *test_case;
|
||||
@ -51,7 +52,7 @@ int main(int argc, char **argv) {
|
||||
int n = 50000;
|
||||
int i;
|
||||
int gen;
|
||||
int pass = 1;
|
||||
int pass;
|
||||
int passed = 0;
|
||||
int failed = 0;
|
||||
|
||||
@ -64,6 +65,7 @@ int main(int argc, char **argv) {
|
||||
fprintf(stdout, "Running tests:\n");
|
||||
|
||||
// Test random number generation
|
||||
pass = 1;
|
||||
fprintf(stdout, "\trandom generation: ");
|
||||
fflush(stdout);
|
||||
for (i = 0; i < 5000000; i++) {
|
||||
@ -79,22 +81,22 @@ int main(int argc, char **argv) {
|
||||
fprintf(stdout, "pass\n");
|
||||
passed++;
|
||||
}
|
||||
pass = 1;
|
||||
|
||||
// Prepare for sort tests
|
||||
for (i = 0; i < n; i++) {
|
||||
test_case[i] = gen_rand(-100, 100);
|
||||
}
|
||||
memcpy(qarray, test_case, sizeof(int) * n);
|
||||
memcpy(test_array, test_case, sizeof(int) * n);
|
||||
qsort(qarray, n, sizeof(int), compar);
|
||||
|
||||
// Test bubble sort
|
||||
pass = 1;
|
||||
memcpy(test_array, test_case, sizeof(int) * n);
|
||||
fprintf(stdout, "\tbubble sort: ");
|
||||
fflush(stdout);
|
||||
bubble_sort(test_array, n);
|
||||
for (i = 0; i < n; i++) {
|
||||
if (test_array[i] != qarray[i] && pass) {
|
||||
if (test_array[i] != qarray[i]) {
|
||||
fprintf(stdout, "fail\n");
|
||||
failed++;
|
||||
pass = 0;
|
||||
@ -105,24 +107,44 @@ int main(int argc, char **argv) {
|
||||
fprintf(stdout, "pass\n");
|
||||
passed++;
|
||||
}
|
||||
pass = 1;
|
||||
|
||||
// Test quick sort
|
||||
fprintf(stdout, "\tquick sort: ");
|
||||
// Test count sort
|
||||
pass = 1;
|
||||
memcpy(test_array, test_case, sizeof(int) * n);
|
||||
fprintf(stdout, "\tcount sort: ");
|
||||
fflush(stdout);
|
||||
quick_sort(test_array, n);
|
||||
count_sort(test_array, n);
|
||||
for (i = 0; i < n; i++) {
|
||||
if (test_array[i] != qarray[i] && pass) {
|
||||
if (test_array[i] != qarray[i]) {
|
||||
fprintf(stdout, "fail\n");
|
||||
failed++;
|
||||
pass = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (pass) {
|
||||
fprintf(stdout, "pass\n");
|
||||
passed++;
|
||||
}
|
||||
|
||||
// Test quick sort
|
||||
pass = 1;
|
||||
memcpy(test_array, test_case, sizeof(int) * n);
|
||||
fprintf(stdout, "\tquick sort: ");
|
||||
fflush(stdout);
|
||||
quick_sort(test_array, n);
|
||||
for (i = 0; i < n; i++) {
|
||||
if (test_array[i] != qarray[i]) {
|
||||
fprintf(stdout, "fail\n");
|
||||
failed++;
|
||||
pass = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (pass) {
|
||||
fprintf(stdout, "pass\n");
|
||||
passed++;
|
||||
}
|
||||
|
||||
fprintf(stdout, "%d tests passed\n", passed);
|
||||
fprintf(stdout, "%d tests failed\n", failed);
|
||||
|
Loading…
Reference in New Issue
Block a user