Browse Source

cleanup files

Chris Cromer 7 months ago
parent
commit
18fc551db3
Signed by: Chris Cromer <chris@cromer.cl> GPG Key ID: 39CC813FF3C8708A
8 changed files with 150 additions and 133 deletions
  1. 1
    1
      Makefile
  2. 62
    34
      src/bitonic_sort.c
  3. 1
    5
      src/include/bitonic_sort.h
  4. 0
    2
      src/include/merge_sort.h
  5. 32
    28
      src/merge_sort.c
  6. 19
    18
      src/selection_sort.c
  7. 34
    44
      src/sort.c
  8. 1
    1
      test/Makefile

+ 1
- 1
Makefile View File

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

+ 62
- 34
src/bitonic_sort.c View File

@@ -13,47 +13,75 @@
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 <bitonic_sort.h>
17
-#define SWAP(x,y) t = x; x = y; y = t;  //definicion del cambio de SWAP que utiliza compare
18
-    int up = 1;
19
-    int down = 0;
16
+#include "swap.h"
20 17
 
21
-void bitonic_sort(int *array, int n){  //funcion bitonic
22
- 	sort(array, n);
18
+/**
19
+ * Comparar y intercambiar los valores para darle orden
20
+ * @param i El primer indice a comparar
21
+ * @param j El segundo indice a comparar
22
+ * @param dir La dirección a ordenar, 1 para ascendentemente o 0 por descendentamente
23
+ * @param array El array a ordenar
24
+ */
25
+void compare(int i, int j, int dir, int *array) { //
26
+	if (dir == (array[i] > array[j])){
27
+		swap(&array[i], &array[j]);
28
+	}
23 29
 }
24 30
 
25
-void compare(int i, int j, int dir, int *array){ //compara y cambia los valores para darle orden
26
-    int t;
31
+/**
32
+ * Unir la secuencia
33
+ * @param low El parte inferior
34
+ * @param c El parte superior
35
+ * @param dir La dirección a ordenar, 1 para ascendentemente o 0 por descendentamente
36
+ * @param array El array a ordenar
37
+ */
38
+void bitonicmerge(int low, int c, int dir, int *array) {
39
+	int i;
40
+	int k;
27 41
 
28
-    if (dir == (array[i] > array[j])){
29
-        SWAP(array[i], array[j]);
30
-    }
42
+	if (c > 1){
43
+		k = c / 2;
44
+		for (i = low; i < low + k; i++){
45
+			compare(i, i + k, dir, array);
46
+		}
47
+		bitonicmerge(low, k, dir, array);
48
+		bitonicmerge(low + k, k, dir, array);
49
+	}
31 50
 }
32 51
 
33
-void bitonicmerge(int low, int c, int dir, int *array){  //ordena la secuenca ascendentemente si dir=1 
34
-    int k, i; 
35
-
36
-    if (c > 1){
37
-         k = c / 2;
38
-        for (i = low;i < low+k ;i++){
39
-            compare(i, i+k, dir, array); 
40
-        }
41
-        bitonicmerge(low, k, dir, array);
42
-        bitonicmerge(low+k, k, dir, array);    
43
-    }
44
-}
52
+/**
53
+ * Generar la secuencia bitonica en forma de piramide
54
+ * @param low El parte inferior
55
+ * @param c El parte superior
56
+ * @param dir La dirección a ordenar, 1 para ascendentemente o 0 por descendentamente
57
+ * @param array El array a ordenar
58
+ */
59
+void recbitonic(int low, int c, int dir, int *array) {
60
+	int k;
45 61
 
46
-void recbitonic(int low, int c, int dir, int *array){ //genera la secuencia bitonica en forma de piramide
47
-    int k;
62
+	if (c > 1){
63
+		k = c / 2;
64
+		recbitonic(low, k, 1, array);
65
+		recbitonic(low + k, k, 0, array);
66
+		bitonicmerge(low, c, dir, array);
67
+	}
68
+}
48 69
 
49
-    if (c > 1){
50
-        k = c / 2;
51
-        recbitonic(low, k, up, array);
52
-        recbitonic(low + k, k, down, array);
53
-        bitonicmerge(low, c, dir, array);
54
-    }
70
+/**
71
+ * Ordenar el arreglo completo
72
+ * @param array El array a ordenar
73
+ * @param n El tamaño del array
74
+ * @param dir La dirección a ordenar, 1 para ascendentemente o 0 por descendentamente
75
+ */
76
+void sort(int *array, int n, int dir) {
77
+	recbitonic(0, n, dir, array);
55 78
 }
56 79
 
57
-void sort(int *array, int n){  //ordena el arreglo completo
58
-	recbitonic(0, n, up, array);
59
-} 
80
+/**
81
+ * Usar el algoritmo de bitonic sort
82
+ * @param array El array a ordenar
83
+ * @param n El tamaño del array
84
+ */
85
+void bitonic_sort(int *array, int n) {
86
+	 sort(array, n, 1);
87
+}

+ 1
- 5
src/include/bitonic_sort.h View File

@@ -16,8 +16,4 @@
16 16
 #ifndef _SORT_BITONIC
17 17
 	#define _SORT_BITONIC
18 18
 	void bitonic_sort(int *array, int n);
19
-	void compare(int i, int j, int dir, int *array);
20
-	void bitonicmerge(int low, int c, int dir, int *array);
21
-	void recbitonic(int low, int c, int dir, int *array);
22
-	void sort(int *array, int n); 
23
-#endif
19
+#endif

+ 0
- 2
src/include/merge_sort.h View File

@@ -16,6 +16,4 @@
16 16
 #ifndef _SORT_MERGE
17 17
 	#define _SORT_MERGE
18 18
 	void merge_sort(int *array, int n);
19
-	void merge_sort_run(int *array, int *temp, int left, int right);
20
-	void merge(int *array, int *temp, int prev_left, int prev_middle, int right);
21 19
 #endif

+ 32
- 28
src/merge_sort.c View File

@@ -13,35 +13,8 @@
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 <stdio.h>
16 17
 #include <stdlib.h>
17
-#include "merge_sort.h"
18
-
19
-/**
20
- * Usar el algoritmo de merge sort
21
- * @param array El array a ordenar
22
- * @param n El tamaño del array
23
- */
24
-void merge_sort(int *array, int n) {
25
-	int *temp = malloc(sizeof(int)* n);
26
-	merge_sort_run(array, temp, 0, n - 1);
27
-	free(temp);
28
-}
29
-
30
-/**
31
- * Correro el merge sort recursivamente
32
- * @param array El array a ordenar
33
- * @param temp Un array temporario para trabajar
34
- * @param left El lado izquerda a ordenar
35
- * @param right El lado derercha a ordenar
36
- */
37
-void merge_sort_run(int *array, int *temp, int left, int right) {
38
-	if (left != right) {
39
-		int middle = (left + right) / 2;
40
-		merge_sort_run(array, temp, left, middle);
41
-		merge_sort_run(array, temp, middle + 1, right);
42
-		merge(array, temp, left, middle + 1, right);
43
-	}
44
-}
45 18
 
46 19
 /**
47 20
  * 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) {
78 51
 		array[left + i] = temp[i];
79 52
 	}
80 53
 }
54
+
55
+/**
56
+ * Correr el merge sort recursivamente
57
+ * @param array El array a ordenar
58
+ * @param temp Un array temporario para trabajar
59
+ * @param left El lado izquerda a ordenar
60
+ * @param right El lado derercha a ordenar
61
+ */
62
+void merge_sort_run(int *array, int *temp, int left, int right) {
63
+	if (left != right) {
64
+		int middle = (left + right) / 2;
65
+		merge_sort_run(array, temp, left, middle);
66
+		merge_sort_run(array, temp, middle + 1, right);
67
+		merge(array, temp, left, middle + 1, right);
68
+	}
69
+}
70
+
71
+/**
72
+ * Usar el algoritmo de merge sort
73
+ * @param array El array a ordenar
74
+ * @param n El tamaño del array
75
+ */
76
+void merge_sort(int *array, int n) {
77
+	int *temp = malloc(sizeof(int)* n);
78
+	if (temp == NULL) {
79
+		fprintf(stderr, "Error: Out of heap space!\n");
80
+		exit(5);
81
+	}
82
+	merge_sort_run(array, temp, 0, n - 1);
83
+	free(temp);
84
+}

+ 19
- 18
src/selection_sort.c View File

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

+ 34
- 44
src/sort.c View File

@@ -139,6 +139,26 @@ void print_invalid_n() {
139 139
 }
140 140
 
141 141
 /**
142
+ * Empezar los pasos antes de ordenar
143
+ */
144
+void start_sort(const char *message, int n) {
145
+	fprintf(stdout, "%s", message);
146
+	fflush(stdout);
147
+	memcpy(work_array, unordered_array, sizeof(int) * n);
148
+	start_timer();
149
+}
150
+
151
+/**
152
+ * Empezar los pasos después de ordenar
153
+ */
154
+void end_sort() {
155
+	stop_timer();
156
+	fprintf(stdout, "done\n");
157
+	print_timer();
158
+	fprintf(stdout, "\n");
159
+}
160
+
161
+/**
142 162
  * Liberar la memoria al salir
143 163
  */
144 164
 void cleanup() {
@@ -306,75 +326,45 @@ int main (int argc, char **argv) {
306 326
 	}
307 327
 
308 328
 	if (merge) {
309
-		fprintf(stdout, "Merge sort corriendo... ");
310
-		fflush(stdout);
311
-		memcpy(work_array, unordered_array, sizeof(int) * n);
312
-		start_timer();
329
+		start_sort("Merge sort corriendo... ", n);
313 330
 		merge_sort(work_array, n);
314
-		stop_timer();
315
-		fprintf(stdout, "done\n");
316
-		print_timer();
331
+		end_sort();
317 332
 	}
318 333
 
319 334
 	if (quick) {
320
-		fprintf(stdout, "Quick sort corriendo... ");
321
-		fflush(stdout);
322
-		memcpy(work_array, unordered_array, sizeof(int) * n);
323
-		start_timer();
335
+		start_sort("Quick sort corriendo... ", n);
324 336
 		quick_sort(work_array, n);
325
-		stop_timer();
326
-		fprintf(stdout, "done\n");
327
-		print_timer();
337
+		end_sort();
328 338
 	}
329 339
 
330 340
 	if (bubble) {
331
-		fprintf(stdout, "Bubble sort corriendo... ");
332
-		fflush(stdout);
333
-		memcpy(work_array, unordered_array, sizeof(int) * n);
334
-		start_timer();
341
+		start_sort("Bubble sort corriendo... ", n);
335 342
 		bubble_sort(work_array, n);
336
-		stop_timer();
337
-		fprintf(stdout, "done\n");
338
-		print_timer();
343
+		end_sort();
339 344
 	}
340 345
 
341 346
 	if (bitonic) {
342
-		fprintf(stdout, "Bitonic sort corriendo... ");
343
-		fflush(stdout);
344
-		memcpy(work_array, unordered_array, sizeof(int) * n);
345
-		start_timer();
347
+		start_sort("Bitonic sort corriendo... ", n);
346 348
 		bitonic_sort(work_array, n);
347
-		stop_timer();
348
-		fprintf(stdout, "done\n");
349
-		print_timer();
349
+		end_sort();
350 350
 	}
351 351
 
352 352
 	if (count) {
353
-		fprintf(stdout, "Count sort corriendo... ");
354
-		fflush(stdout);
355
-		memcpy(work_array, unordered_array, sizeof(int) * n);
356
-		start_timer();
353
+		start_sort("Count sort corriendo... ", n);
357 354
 		count_sort(work_array, n);
358
-		stop_timer();
359
-		fprintf(stdout, "done\n");
360
-		print_timer();
355
+		end_sort();
361 356
 	}
362 357
 
363 358
 	if (selection) {
364
-		fprintf(stdout, "Selection sort corriendo... ");
365
-		fflush(stdout);
366
-		memcpy(work_array, unordered_array, sizeof(int) * n);
367
-		start_timer();
359
+		start_sort("Selection sort corriendo... ", n);
368 360
 		selection_sort(work_array, n);
369
-		stop_timer();
370
-		fprintf(stdout, "done\n");
371
-		print_timer();
361
+		end_sort();
372 362
 	}
373 363
 
374 364
 	if (imprimir) {
375
-		fprintf(stdout, "\nAntes:\n");
365
+		fprintf(stdout, "Antes:\n");
376 366
 		print_array(unordered_array, n);
377
-		fprintf(stdout, "\nDespués:\n");
367
+		fprintf(stdout, "Después:\n");
378 368
 		print_array(work_array, n);
379 369
 	}
380 370
 	return 0;

+ 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/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/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
7 7
 
8 8
 all: test
9 9
 

Loading…
Cancel
Save