Browse Source

various improvements

Chris Cromer 7 months ago
parent
commit
ca7e0a9be8
Signed by: Chris Cromer <chris@cromer.cl> GPG Key ID: 39CC813FF3C8708A
9 changed files with 41 additions and 93 deletions
  1. 2
    2
      Makefile
  2. 5
    4
      src/bitonic_sort.c
  3. 1
    0
      src/count_sort.c
  4. 0
    19
      src/include/swap.h
  5. 5
    4
      src/selection_sort.c
  6. 7
    7
      src/sort.c
  7. 0
    28
      src/swap.c
  8. 20
    28
      src/timer.c
  9. 1
    1
      test/Makefile

+ 2
- 2
Makefile View File

@@ -1,7 +1,7 @@
1 1
 CC=gcc
2 2
 CFLAGS=-Wall -Isrc/include -DDEBUG -g
3
-#LDFLAGS=-lm
4
-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
3
+LDFLAGS=-lm
4
+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
5 5
 OBJ=$(SRC:.c=.o)
6 6
 
7 7
 all: sort informe

+ 5
- 4
src/bitonic_sort.c View File

@@ -13,8 +13,6 @@
13 13
  * 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.
14 14
  */
15 15
 
16
-#include "swap.h"
17
-
18 16
 /**
19 17
  * Verificar si x es de 2^n
20 18
  * @param x El valor a verificar
@@ -55,9 +53,12 @@ int greatest_power_of_two_less_than(int n) {
55 53
  * @param dir La dirección a ordenar, 1 para ascendentemente o 0 por descendentamente
56 54
  * @param array El array a ordenar
57 55
  */
58
-void compare(int i, int j, int dir, int *array) { //
56
+void compare(int i, int j, int dir, int *array) {
57
+	int temp;
59 58
 	if (dir == (array[i] > array[j])){
60
-		swap(&array[i], &array[j]);
59
+		temp = array[i];
60
+		array[i] = array[j];
61
+		array[j] = temp;
61 62
 	}
62 63
 }
63 64
 

+ 1
- 0
src/count_sort.c View File

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

+ 0
- 19
src/include/swap.h View File

@@ -1,19 +0,0 @@
1
-/*
2
- * Copyright 2018 Christopher Cromer
3
- * Copyright 2018 Rodolfo Cuevas
4
- *
5
- * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
- *
7
- * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
- *
9
- * 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.
10
- *
11
- * 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.
12
- *
13
- * 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.
14
- */
15
-
16
-#ifndef _SORT_SWAP
17
-	#define _SORT_SWAP
18
-	void swap(int *xp, int *yp);
19
-#endif

+ 5
- 4
src/selection_sort.c View File

@@ -13,8 +13,6 @@
13 13
  * 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.
14 14
  */
15 15
 
16
-#include "swap.h"
17
-
18 16
 /**
19 17
  * Usar el algoritmo de ordenamentio por selección
20 18
  * @param array El array a ordenar
@@ -23,14 +21,17 @@
23 21
 void selection_sort(int *array, int n) {
24 22
 	int i;
25 23
 	int j;
24
+	int temp;
26 25
 	int min_idx;
27
-	for (i = 0; i < n-1; i++) {
26
+	for (i = 0; i < n - 1; i++) {
28 27
 		min_idx = i;
29 28
 		for (j = i + 1; j < n; j++) {
30 29
 			if (array[j] < array[min_idx]) {
31 30
 				min_idx = j;
32 31
 			}
33 32
 		}
34
-		swap(&array[min_idx], &array[i]);
33
+		temp = array[min_idx];
34
+		array[min_idx] = array[i];
35
+		array[i] = temp;
35 36
 	}
36 37
 }

+ 7
- 7
src/sort.c View File

@@ -346,7 +346,7 @@ int main (int argc, char **argv) {
346 346
 		end_sort();
347 347
 	}
348 348
 
349
-	// O(n+k)
349
+	// O(n^2)
350 350
 	if (count) {
351 351
 		start_sort("Count sort corriendo... ", n);
352 352
 		count_sort(work_array, n);
@@ -354,16 +354,16 @@ int main (int argc, char **argv) {
354 354
 	}
355 355
 
356 356
 	// O(n^2)
357
-	if (bubble) {
358
-		start_sort("Bubble sort corriendo... ", n);
359
-		bubble_sort(work_array, n);
357
+	if (selection) {
358
+		start_sort("Selection sort corriendo... ", n);
359
+		selection_sort(work_array, n);
360 360
 		end_sort();
361 361
 	}
362 362
 
363 363
 	// O(n^2)
364
-	if (selection) {
365
-		start_sort("Selection sort corriendo... ", n);
366
-		selection_sort(work_array, n);
364
+	if (bubble) {
365
+		start_sort("Bubble sort corriendo... ", n);
366
+		bubble_sort(work_array, n);
367 367
 		end_sort();
368 368
 	}
369 369
 

+ 0
- 28
src/swap.c View File

@@ -1,28 +0,0 @@
1
-/*
2
- * Copyright 2018 Christopher Cromer
3
- * Copyright 2018 Rodolfo Cuevas
4
- *
5
- * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
- *
7
- * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
- *
9
- * 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.
10
- *
11
- * 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.
12
- *
13
- * 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.
14
- */
15
-
16
-/**
17
- * Intercambiar dos valores
18
- * @param xp Primer valor
19
- * @param yp Segundo valor
20
- */
21
-void swap(int *xp, int *yp) {
22
-	if (xp == yp) {
23
-		return;
24
-	}
25
-	*xp = *xp + *yp;
26
-	*yp = *xp - *yp;
27
-	*xp = *xp - *yp;
28
-}

+ 20
- 28
src/timer.c View File

@@ -15,33 +15,34 @@
15 15
 
16 16
 #include <stdio.h>
17 17
 #include <time.h>
18
+#include <math.h>
18 19
 
19 20
 /**
20 21
  * Cuando empezó el timer
21 22
  */
22
-static time_t start_time = 0;
23
+static struct timespec start_time;
23 24
 /**
24 25
  * Cuando terminó el timer
25 26
  */
26
-static time_t stop_time = 0;
27
+static struct timespec stop_time;
27 28
 
28 29
 /**
29 30
  * Empezar el timer
30 31
  */
31 32
 void start_timer() {
32
-	time(&start_time);
33
-	stop_time = 0;
33
+	clock_gettime(CLOCK_MONOTONIC, &start_time);
34
+	stop_time.tv_nsec = 0;
34 35
 }
35 36
 
36 37
 /**
37 38
  * Terminar el timer
38 39
  */
39 40
 void stop_timer() {
40
-	if (start_time == 0) {
41
+	if (start_time.tv_nsec == 0) {
41 42
 		fprintf(stderr, "Error: El temporizador no ha comenzado todavía!\n");
42 43
 	}
43 44
 	else {
44
-		time(&stop_time);
45
+		clock_gettime(CLOCK_MONOTONIC, &stop_time);
45 46
 	}
46 47
 }
47 48
 
@@ -49,16 +50,21 @@ void stop_timer() {
49 50
  * Imprimir el tiempo de ejecución
50 51
  */
51 52
 void print_timer() {
52
-	if (start_time == 0) {
53
+	if (start_time.tv_nsec == 0) {
53 54
 		fprintf(stderr, "Error: El temporizador no ha comenzado todavía!\n");
54 55
 	}
55
-	else if (stop_time == 0) {
56
+	else if (stop_time.tv_nsec == 0) {
56 57
 		fprintf(stderr, "Error: El temporizador no ha terminado todavía!\n");
57 58
 	}
58 59
 	else {
59
-		long int seconds = (long int) (stop_time - start_time);
60
-		long int minutes = 0;
61
-		long int hours = 0;
60
+		time_t seconds  = stop_time.tv_sec - start_time.tv_sec;
61
+		unsigned long long milliseconds = round((stop_time.tv_nsec - start_time.tv_nsec)/ 1.0e6); // Convert nanoseconds to milliseconds
62
+		if (milliseconds > 999) {
63
+			seconds++;
64
+			milliseconds = 0;
65
+		}
66
+		unsigned int minutes = 0;
67
+		unsigned int hours = 0;
62 68
 		if (seconds >= 3600) {
63 69
 			hours = seconds / 3600;
64 70
 			seconds = seconds - (hours * 3600);
@@ -66,29 +72,15 @@ void print_timer() {
66 72
 				minutes = seconds / 60;
67 73
 				seconds = seconds - (minutes * 60);
68 74
 			}
69
-			fprintf(stdout, "Tiempo de ejecución: %ld horas, %ld minutos y %ld segundos\n", hours, minutes, seconds);
75
+			fprintf(stdout, "Tiempo de ejecución: %d horas, %d minutos y %ld.%lld segundos\n", hours, minutes, seconds, milliseconds);
70 76
 		}
71 77
 		else if (seconds >= 60) {
72 78
 			minutes = seconds / 60;
73 79
 			seconds = seconds - (minutes * 60);
74
-			fprintf(stdout, "Tiempo de ejecución: %ld minutos y %ld segundos\n", minutes, seconds);
80
+			fprintf(stdout, "Tiempo de ejecución: %d minutos y %ld.%lld segundos\n", minutes, seconds, milliseconds);
75 81
 		}
76 82
 		else {
77
-			fprintf(stdout, "Tiempo de ejecución: %ld segundos\n", seconds);
83
+			fprintf(stdout, "Tiempo de ejecución: %ld.%lld segundos\n", seconds, milliseconds);
78 84
 		}
79 85
 	}
80 86
 }
81
-
82
-/**
83
- * Devolver el tiempo que demoró en ejecutarse
84
- * @return El tiempo que demoró
85
- */
86
-long int get_timer() {
87
-	if (start_time == 0) {
88
-		fprintf(stderr, "Error: El temporizador no ha comenzado todavía!\n");
89
-	}
90
-	else if (stop_time == 0) {
91
-		fprintf(stderr, "Error: El temporizador no ha terminado todavía!\n");
92
-	}
93
-	return (long int) (stop_time - start_time);
94
-}

+ 1
- 1
test/Makefile View File

@@ -3,7 +3,7 @@ CFLAGS=-Wall -I../src/include -DDEBUG -g
3 3
 SRC=test.c
4 4
 OBJ=$(SRC:.c=.o)
5 5
 
6
-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
6
+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
7 7
 
8 8
 all: test
9 9
 

Loading…
Cancel
Save