216 lines
6.0 KiB
C
216 lines
6.0 KiB
C
/*
|
|
* 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 <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <getopt.h>
|
|
#include "random.h"
|
|
#include "timer.h"
|
|
#include "bubble_sort.h"
|
|
|
|
#define SORT_VERSION "1.0.0"
|
|
|
|
/**
|
|
* Imprimir el uso del programa
|
|
*/
|
|
void print_usage() {
|
|
fprintf(stdout, "uso: sort [OPCIÓN]\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");
|
|
fprintf(stdout, " -B, --bitonic usar bitonic sort\n");
|
|
fprintf(stdout, " -c, --count usar ordenamiento por conteo\n");
|
|
fprintf(stdout, " -s, --selection usar ordenamiento por selección\n");
|
|
fprintf(stdout, " -n, --n=N la cantidad de elementos a ordenar, la\n");
|
|
fprintf(stdout, " cantidad predeterminado es 10\n");
|
|
fprintf(stdout, " -e, --elegir el usuario debe elegir los \"n\" valores de\n");
|
|
fprintf(stdout, " elementos a ordenar, sin esta opción los\n");
|
|
fprintf(stdout, " valores son elegido por el programa al azar\n");
|
|
fprintf(stdout, " -i, --imprimir imprimir el array antes y despues de ordenar\n");
|
|
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);
|
|
}
|
|
|
|
void print_array(int *array, int n) {
|
|
int i;
|
|
for (i = 0; i < n; i++) {
|
|
fprintf(stdout, "%d ", array[i]);
|
|
}
|
|
fprintf(stdout, "\n");
|
|
}
|
|
|
|
/**
|
|
* La entrada del programa
|
|
* @param argc La cantidad de argumentos pasado al programa
|
|
* @return Retorna el codigo de error o 0 por exito
|
|
*/
|
|
int main (int argc, char **argv) {
|
|
long long i;
|
|
int n = 10;
|
|
int elegir = 0;
|
|
int imprimir = 0;
|
|
int algoritmo = -1;
|
|
int opt;
|
|
int long_index = 0;
|
|
static struct option long_options[] = {
|
|
{"merge", no_argument, 0, 'm'},
|
|
{"quick", no_argument, 0, 'q'},
|
|
{"bubble", no_argument, 0, 'b'},
|
|
{"bitonic", no_argument, 0, 'B'},
|
|
{"count", no_argument, 0, 'c'},
|
|
{"selection", no_argument, 0, 's'},
|
|
{"n", required_argument, 0, 'n'},
|
|
{"elegir", no_argument, 0, 'e'},
|
|
{"imprimir", no_argument, 0, 'i'},
|
|
{"version", no_argument, 0, 'v'},
|
|
{0, 0, 0, 0}
|
|
};
|
|
|
|
if (argc == 1) {
|
|
print_usage();
|
|
return 0;
|
|
}
|
|
|
|
while ((opt = getopt_long(argc, argv, "mqbBcsn:ev", long_options, &long_index)) != -1) {
|
|
switch (opt) {
|
|
case 'm':
|
|
if (algoritmo != -1) {
|
|
extra_algorithm();
|
|
}
|
|
algoritmo = 0;
|
|
break;
|
|
case 'q':
|
|
if (algoritmo != -1) {
|
|
extra_algorithm();
|
|
}
|
|
algoritmo = 1;
|
|
break;
|
|
case 'b':
|
|
if (algoritmo != -1) {
|
|
extra_algorithm();
|
|
}
|
|
algoritmo = 2;
|
|
break;
|
|
case 'B':
|
|
if (algoritmo != -1) {
|
|
extra_algorithm();
|
|
}
|
|
algoritmo = 3;
|
|
break;
|
|
case 'c':
|
|
if (algoritmo != -1) {
|
|
extra_algorithm();
|
|
}
|
|
algoritmo = 4;
|
|
break;
|
|
case 's':
|
|
if (algoritmo != -1) {
|
|
extra_algorithm();
|
|
}
|
|
algoritmo = 5;
|
|
break;
|
|
case 'n':
|
|
n = atol(optarg);
|
|
if (n <= 1) {
|
|
fprintf(stderr, "Error: n tiene que ser mayor de 1!\n");
|
|
return 3;
|
|
}
|
|
break;
|
|
case 'e':
|
|
elegir = 1;
|
|
break;
|
|
case 'i':
|
|
imprimir = 1;
|
|
break;
|
|
case 'v':
|
|
printf("sort versión: %s\n", SORT_VERSION);
|
|
return 0;
|
|
break;
|
|
default:
|
|
print_usage();
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
int *array = malloc(sizeof(int) * n);
|
|
|
|
// Llenar el array con valores para ordenar después
|
|
for (i = 0; i < n; i++) {
|
|
if (elegir) {
|
|
opt = 0;
|
|
while (opt <= 1) {
|
|
fprintf(stdout, "Elegir elemento %lli: ", i + 1);
|
|
fscanf(stdin, "%d", &opt);
|
|
if (opt <= 1) {
|
|
fprintf(stdout, "Número invalido! Tiene que ser mayor de 1!\n");
|
|
}
|
|
}
|
|
array[i] = opt;
|
|
}
|
|
else {
|
|
array[i] = gen_rand(-1000, 5000);
|
|
}
|
|
}
|
|
|
|
if (imprimir) {
|
|
fprintf(stdout, "\nAntes:\n");
|
|
print_array(array, n);
|
|
}
|
|
|
|
if (algoritmo == 0) {
|
|
// merge sort
|
|
}
|
|
else if (algoritmo == 1) {
|
|
// quick sort
|
|
}
|
|
else if (algoritmo == 2) {
|
|
start_timer();
|
|
bubble_sort(array, n);
|
|
stop_timer();
|
|
}
|
|
else if (algoritmo == 3) {
|
|
// bitonic sort
|
|
}
|
|
else if (algoritmo == 4) {
|
|
// count sort
|
|
}
|
|
else if (algoritmo == 5) {
|
|
// selection sort
|
|
}
|
|
else {
|
|
fprintf(stderr, "Error: No se seleccionó un algoritmo valido!\n");
|
|
print_usage();
|
|
return 4;
|
|
}
|
|
|
|
if (imprimir) {
|
|
fprintf(stdout, "\nDespués:\n");
|
|
print_array(array, n);
|
|
}
|
|
|
|
free(array);
|
|
|
|
print_timer();
|
|
return 0;
|
|
}
|