Algorithm improvements #27

Merged
cromer merged 1 commits from improvealgos into master 2018-11-18 18:03:05 -03:00
9 changed files with 41 additions and 93 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/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
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
OBJ=$(SRC:.c=.o)
all: sort informe

View File

@ -13,8 +13,6 @@
* 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 "swap.h"
/**
* Verificar si x es de 2^n
* @param x El valor a verificar
@ -55,9 +53,12 @@ int greatest_power_of_two_less_than(int n) {
* @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) { //
void compare(int i, int j, int dir, int *array) {
int temp;
if (dir == (array[i] > array[j])){
swap(&array[i], &array[j]);
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}

View File

@ -35,6 +35,7 @@ void count_sort(int *array, int n) {
fprintf(stderr, "Error: Out of heap space!\n");
exit(5);
}
memset(count, 0, sizeof(int) *n);
memcpy(temp, array, sizeof(int) * n);
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {

View File

@ -1,19 +0,0 @@
/*
* 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,8 +13,6 @@
* 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 "swap.h"
/**
* Usar el algoritmo de ordenamentio por selección
* @param array El array a ordenar
@ -23,14 +21,17 @@
void selection_sort(int *array, int n) {
int i;
int j;
int temp;
int min_idx;
for (i = 0; i < n-1; i++) {
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]);
temp = array[min_idx];
array[min_idx] = array[i];
array[i] = temp;
}
}

View File

@ -346,7 +346,7 @@ int main (int argc, char **argv) {
end_sort();
}
// O(n+k)
// O(n^2)
if (count) {
start_sort("Count sort corriendo... ", n);
count_sort(work_array, n);
@ -354,16 +354,16 @@ int main (int argc, char **argv) {
}
// O(n^2)
if (bubble) {
start_sort("Bubble sort corriendo... ", n);
bubble_sort(work_array, n);
if (selection) {
start_sort("Selection sort corriendo... ", n);
selection_sort(work_array, n);
end_sort();
}
// O(n^2)
if (selection) {
start_sort("Selection sort corriendo... ", n);
selection_sort(work_array, n);
if (bubble) {
start_sort("Bubble sort corriendo... ", n);
bubble_sort(work_array, n);
end_sort();
}

View File

@ -1,28 +0,0 @@
/*
* 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) {
if (xp == yp) {
return;
}
*xp = *xp + *yp;
*yp = *xp - *yp;
*xp = *xp - *yp;
}

View File

@ -15,33 +15,34 @@
#include <stdio.h>
#include <time.h>
#include <math.h>
/**
* Cuando empezó el timer
*/
static time_t start_time = 0;
static struct timespec start_time;
/**
* Cuando terminó el timer
*/
static time_t stop_time = 0;
static struct timespec stop_time;
/**
* Empezar el timer
*/
void start_timer() {
time(&start_time);
stop_time = 0;
clock_gettime(CLOCK_MONOTONIC, &start_time);
stop_time.tv_nsec = 0;
}
/**
* Terminar el timer
*/
void stop_timer() {
if (start_time == 0) {
if (start_time.tv_nsec == 0) {
fprintf(stderr, "Error: El temporizador no ha comenzado todavía!\n");
}
else {
time(&stop_time);
clock_gettime(CLOCK_MONOTONIC, &stop_time);
}
}
@ -49,16 +50,21 @@ void stop_timer() {
* Imprimir el tiempo de ejecución
*/
void print_timer() {
if (start_time == 0) {
if (start_time.tv_nsec == 0) {
fprintf(stderr, "Error: El temporizador no ha comenzado todavía!\n");
}
else if (stop_time == 0) {
else if (stop_time.tv_nsec == 0) {
fprintf(stderr, "Error: El temporizador no ha terminado todavía!\n");
}
else {
long int seconds = (long int) (stop_time - start_time);
long int minutes = 0;
long int hours = 0;
time_t seconds = stop_time.tv_sec - start_time.tv_sec;
unsigned long long milliseconds = round((stop_time.tv_nsec - start_time.tv_nsec)/ 1.0e6); // Convert nanoseconds to milliseconds
if (milliseconds > 999) {
seconds++;
milliseconds = 0;
}
unsigned int minutes = 0;
unsigned int hours = 0;
if (seconds >= 3600) {
hours = seconds / 3600;
seconds = seconds - (hours * 3600);
@ -66,29 +72,15 @@ void print_timer() {
minutes = seconds / 60;
seconds = seconds - (minutes * 60);
}
fprintf(stdout, "Tiempo de ejecución: %ld horas, %ld minutos y %ld segundos\n", hours, minutes, seconds);
fprintf(stdout, "Tiempo de ejecución: %d horas, %d minutos y %ld.%lld segundos\n", hours, minutes, seconds, milliseconds);
}
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);
fprintf(stdout, "Tiempo de ejecución: %d minutos y %ld.%lld segundos\n", minutes, seconds, milliseconds);
}
else {
fprintf(stdout, "Tiempo de ejecución: %ld segundos\n", seconds);
fprintf(stdout, "Tiempo de ejecución: %ld.%lld segundos\n", seconds, milliseconds);
}
}
}
/**
* Devolver el tiempo que demoró en ejecutarse
* @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/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
OBJ+=../src/random.o ../src/bubble_sort.o ../src/count_sort.o ../src/quick_sort.o ../src/merge_sort.o ../src/bitonic_sort.o ../src/selection_sort.o
all: test