Browse Source

Merge branch 'selection_sort' of UBB/sort into master

Chris Cromer 7 months ago
parent
commit
c4d22ac4da
8 changed files with 199 additions and 6 deletions
  1. 1
    1
      Makefile
  2. 59
    0
      src/bitonic_sort.c
  3. 23
    0
      src/include/bitonic_sort.h
  4. 19
    0
      src/include/selection_sort.h
  5. 35
    0
      src/selection_sort.c
  6. 18
    2
      src/sort.c
  7. 2
    1
      test/Makefile
  8. 42
    2
      test/test.c

+ 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
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

+ 59
- 0
src/bitonic_sort.c View File

@@ -0,0 +1,59 @@
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
+#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;
20
+
21
+void bitonic_sort(int *array, int n){  //funcion bitonic
22
+ 	sort(array, n);
23
+}
24
+
25
+void compare(int i, int j, int dir, int *array){ //compara y cambia los valores para darle orden
26
+    int t;
27
+
28
+    if (dir == (array[i] > array[j])){
29
+        SWAP(array[i], array[j]);
30
+    }
31
+}
32
+
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
+}
45
+
46
+void recbitonic(int low, int c, int dir, int *array){ //genera la secuencia bitonica en forma de piramide
47
+    int k;
48
+
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
+    }
55
+}
56
+
57
+void sort(int *array, int n){  //ordena el arreglo completo
58
+	recbitonic(0, n, up, array);
59
+} 

+ 23
- 0
src/include/bitonic_sort.h View File

@@ -0,0 +1,23 @@
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_BITONIC
17
+	#define _SORT_BITONIC
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
- 0
src/include/selection_sort.h View File

@@ -0,0 +1,19 @@
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_SELECTION
17
+	#define _SORT_SELECTION
18
+	void selection_sort(int *array, int n);
19
+#endif

+ 35
- 0
src/selection_sort.c View File

@@ -0,0 +1,35 @@
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
+void swap(int *xp, int *yp){
16
+	int temp = *xp;
17
+	*xp = *yp;
18
+	*yp = temp;
19
+} 
20
+
21
+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
+    }
34
+}
35
+

+ 18
- 2
src/sort.c View File

@@ -24,6 +24,8 @@
24 24
 #include "bubble_sort.h"
25 25
 #include "count_sort.h"
26 26
 #include "quick_sort.h"
27
+#include "bitonic_sort.h"
28
+#include "selection_sort.h"
27 29
 #include "merge_sort.h"
28 30
 
29 31
 #define SORT_VERSION "1.0.0"
@@ -337,7 +339,14 @@ int main (int argc, char **argv) {
337 339
 	}
338 340
 
339 341
 	if (bitonic) {
340
-		// bitonic sort
342
+		fprintf(stdout, "Bitonic sort corriendo... ");
343
+		fflush(stdout);
344
+		memcpy(work_array, unordered_array, sizeof(int) * n);
345
+		start_timer();
346
+		bitonic_sort(work_array, n);
347
+		stop_timer();
348
+		fprintf(stdout, "done\n");
349
+		print_timer();
341 350
 	}
342 351
 
343 352
 	if (count) {
@@ -352,7 +361,14 @@ int main (int argc, char **argv) {
352 361
 	}
353 362
 
354 363
 	if (selection) {
355
-		// selection sort
364
+		fprintf(stdout, "Selection sort corriendo... ");
365
+		fflush(stdout);
366
+		memcpy(work_array, unordered_array, sizeof(int) * n);
367
+		start_timer();
368
+		selection_sort(work_array, n);
369
+		stop_timer();
370
+		fprintf(stdout, "done\n");
371
+		print_timer();
356 372
 	}
357 373
 
358 374
 	if (imprimir) {

+ 2
- 1
test/Makefile View File

@@ -2,7 +2,8 @@ CC=gcc
2 2
 CFLAGS=-Wall -I../src/include -DDEBUG -g
3 3
 SRC=test.c
4 4
 OBJ=$(SRC:.c=.o)
5
-OBJ+=../src/random.o ../src/bubble_sort.o ../src/timer.o ../src/count_sort.o ../src/quick_sort.o ../src/merge_sort.o
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 7
 
7 8
 all: test
8 9
 

+ 42
- 2
test/test.c View File

@@ -20,8 +20,10 @@
20 20
 #include "random.h"
21 21
 #include "bubble_sort.h"
22 22
 #include "count_sort.h"
23
-#include "quick_sort.h"
23
+#include "selection_sort.h"
24
+#include "bitonic_sort.h"
24 25
 #include "merge_sort.h"
26
+#include "quick_sort.h"
25 27
 
26 28
 static int *test_case;
27 29
 static int *test_array;
@@ -50,7 +52,7 @@ void cleanup() {
50 52
  * El programa de test
51 53
  */
52 54
 int main(int argc, char **argv) {
53
-	int n = 50000;
55
+	int n = 32768;
54 56
 	int i;
55 57
 	int gen;
56 58
 	int pass;
@@ -159,6 +161,44 @@ int main(int argc, char **argv) {
159 161
 		passed++;
160 162
 	}
161 163
 
164
+	 // Test bitonic sort
165
+	pass = 1;
166
+	memcpy(test_array, test_case, sizeof(int) * n);
167
+	fprintf(stdout, "\tbitonic sort: ");
168
+	fflush(stdout);
169
+	bitonic_sort(test_array, n);
170
+	for (i = 0; i < n; i++) {
171
+		if (test_array[i] != qarray[i]) {
172
+			fprintf(stdout, "fail\n");
173
+			failed++;
174
+			pass = 0;
175
+			break;
176
+		}
177
+	}
178
+	if (pass) {
179
+		fprintf(stdout, "pass\n");
180
+		passed++;
181
+	}
182
+
183
+	// Test selection sort
184
+	pass = 1;
185
+	memcpy(test_array, test_case, sizeof(int) * n);
186
+	fprintf(stdout, "\tselection sort: ");
187
+	fflush(stdout);
188
+	selection_sort(test_array, n);
189
+	for (i = 0; i < n; i++) {
190
+		if (test_array[i] != qarray[i]) {
191
+			fprintf(stdout, "fail\n");
192
+			failed++;
193
+			pass = 0;
194
+			break;
195
+		}
196
+	}
197
+	if (pass) {
198
+		fprintf(stdout, "pass\n");
199
+		passed++;
200
+	}
201
+
162 202
 	// Test merge sort
163 203
 	pass = 1;
164 204
 	memcpy(test_array, test_case, sizeof(int) * n);

Loading…
Cancel
Save