Merge branch 'selection_sort' of UBB/sort into master
This commit is contained in:
commit
c4d22ac4da
2
Makefile
2
Makefile
@ -1,7 +1,7 @@
|
|||||||
CC=gcc
|
CC=gcc
|
||||||
CFLAGS=-Wall -Isrc/include -DDEBUG -g
|
CFLAGS=-Wall -Isrc/include -DDEBUG -g
|
||||||
#LDFLAGS=-lm
|
#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=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)
|
OBJ=$(SRC:.c=.o)
|
||||||
|
|
||||||
all: sort informe
|
all: sort informe
|
||||||
|
59
src/bitonic_sort.c
Normal file
59
src/bitonic_sort.c
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* 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 <bitonic_sort.h>
|
||||||
|
#define SWAP(x,y) t = x; x = y; y = t; //definicion del cambio de SWAP que utiliza compare
|
||||||
|
int up = 1;
|
||||||
|
int down = 0;
|
||||||
|
|
||||||
|
void bitonic_sort(int *array, int n){ //funcion bitonic
|
||||||
|
sort(array, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
void compare(int i, int j, int dir, int *array){ //compara y cambia los valores para darle orden
|
||||||
|
int t;
|
||||||
|
|
||||||
|
if (dir == (array[i] > array[j])){
|
||||||
|
SWAP(array[i], array[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bitonicmerge(int low, int c, int dir, int *array){ //ordena la secuenca ascendentemente si dir=1
|
||||||
|
int k, i;
|
||||||
|
|
||||||
|
if (c > 1){
|
||||||
|
k = c / 2;
|
||||||
|
for (i = low;i < low+k ;i++){
|
||||||
|
compare(i, i+k, dir, array);
|
||||||
|
}
|
||||||
|
bitonicmerge(low, k, dir, array);
|
||||||
|
bitonicmerge(low+k, k, dir, array);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void recbitonic(int low, int c, int dir, int *array){ //genera la secuencia bitonica en forma de piramide
|
||||||
|
int k;
|
||||||
|
|
||||||
|
if (c > 1){
|
||||||
|
k = c / 2;
|
||||||
|
recbitonic(low, k, up, array);
|
||||||
|
recbitonic(low + k, k, down, array);
|
||||||
|
bitonicmerge(low, c, dir, array);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sort(int *array, int n){ //ordena el arreglo completo
|
||||||
|
recbitonic(0, n, up, array);
|
||||||
|
}
|
23
src/include/bitonic_sort.h
Normal file
23
src/include/bitonic_sort.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* 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_BITONIC
|
||||||
|
#define _SORT_BITONIC
|
||||||
|
void bitonic_sort(int *array, int n);
|
||||||
|
void compare(int i, int j, int dir, int *array);
|
||||||
|
void bitonicmerge(int low, int c, int dir, int *array);
|
||||||
|
void recbitonic(int low, int c, int dir, int *array);
|
||||||
|
void sort(int *array, int n);
|
||||||
|
#endif
|
19
src/include/selection_sort.h
Normal file
19
src/include/selection_sort.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* 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_SELECTION
|
||||||
|
#define _SORT_SELECTION
|
||||||
|
void selection_sort(int *array, int n);
|
||||||
|
#endif
|
35
src/selection_sort.c
Normal file
35
src/selection_sort.c
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
void swap(int *xp, int *yp){
|
||||||
|
int temp = *xp;
|
||||||
|
*xp = *yp;
|
||||||
|
*yp = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void selection_sort(int *array, int n) {
|
||||||
|
int i, j, min_idx;
|
||||||
|
|
||||||
|
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
20
src/sort.c
20
src/sort.c
@ -24,6 +24,8 @@
|
|||||||
#include "bubble_sort.h"
|
#include "bubble_sort.h"
|
||||||
#include "count_sort.h"
|
#include "count_sort.h"
|
||||||
#include "quick_sort.h"
|
#include "quick_sort.h"
|
||||||
|
#include "bitonic_sort.h"
|
||||||
|
#include "selection_sort.h"
|
||||||
#include "merge_sort.h"
|
#include "merge_sort.h"
|
||||||
|
|
||||||
#define SORT_VERSION "1.0.0"
|
#define SORT_VERSION "1.0.0"
|
||||||
@ -337,7 +339,14 @@ int main (int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (bitonic) {
|
if (bitonic) {
|
||||||
// bitonic sort
|
fprintf(stdout, "Bitonic sort corriendo... ");
|
||||||
|
fflush(stdout);
|
||||||
|
memcpy(work_array, unordered_array, sizeof(int) * n);
|
||||||
|
start_timer();
|
||||||
|
bitonic_sort(work_array, n);
|
||||||
|
stop_timer();
|
||||||
|
fprintf(stdout, "done\n");
|
||||||
|
print_timer();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count) {
|
if (count) {
|
||||||
@ -352,7 +361,14 @@ int main (int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (selection) {
|
if (selection) {
|
||||||
// selection sort
|
fprintf(stdout, "Selection sort corriendo... ");
|
||||||
|
fflush(stdout);
|
||||||
|
memcpy(work_array, unordered_array, sizeof(int) * n);
|
||||||
|
start_timer();
|
||||||
|
selection_sort(work_array, n);
|
||||||
|
stop_timer();
|
||||||
|
fprintf(stdout, "done\n");
|
||||||
|
print_timer();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (imprimir) {
|
if (imprimir) {
|
||||||
|
@ -2,7 +2,8 @@ CC=gcc
|
|||||||
CFLAGS=-Wall -I../src/include -DDEBUG -g
|
CFLAGS=-Wall -I../src/include -DDEBUG -g
|
||||||
SRC=test.c
|
SRC=test.c
|
||||||
OBJ=$(SRC:.c=.o)
|
OBJ=$(SRC:.c=.o)
|
||||||
OBJ+=../src/random.o ../src/bubble_sort.o ../src/timer.o ../src/count_sort.o ../src/quick_sort.o ../src/merge_sort.o
|
|
||||||
|
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
|
||||||
|
|
||||||
all: test
|
all: test
|
||||||
|
|
||||||
|
44
test/test.c
44
test/test.c
@ -20,8 +20,10 @@
|
|||||||
#include "random.h"
|
#include "random.h"
|
||||||
#include "bubble_sort.h"
|
#include "bubble_sort.h"
|
||||||
#include "count_sort.h"
|
#include "count_sort.h"
|
||||||
#include "quick_sort.h"
|
#include "selection_sort.h"
|
||||||
|
#include "bitonic_sort.h"
|
||||||
#include "merge_sort.h"
|
#include "merge_sort.h"
|
||||||
|
#include "quick_sort.h"
|
||||||
|
|
||||||
static int *test_case;
|
static int *test_case;
|
||||||
static int *test_array;
|
static int *test_array;
|
||||||
@ -50,7 +52,7 @@ void cleanup() {
|
|||||||
* El programa de test
|
* El programa de test
|
||||||
*/
|
*/
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
int n = 50000;
|
int n = 32768;
|
||||||
int i;
|
int i;
|
||||||
int gen;
|
int gen;
|
||||||
int pass;
|
int pass;
|
||||||
@ -154,6 +156,44 @@ int main(int argc, char **argv) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (pass) {
|
||||||
|
fprintf(stdout, "pass\n");
|
||||||
|
passed++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test bitonic sort
|
||||||
|
pass = 1;
|
||||||
|
memcpy(test_array, test_case, sizeof(int) * n);
|
||||||
|
fprintf(stdout, "\tbitonic sort: ");
|
||||||
|
fflush(stdout);
|
||||||
|
bitonic_sort(test_array, n);
|
||||||
|
for (i = 0; i < n; i++) {
|
||||||
|
if (test_array[i] != qarray[i]) {
|
||||||
|
fprintf(stdout, "fail\n");
|
||||||
|
failed++;
|
||||||
|
pass = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (pass) {
|
||||||
|
fprintf(stdout, "pass\n");
|
||||||
|
passed++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test selection sort
|
||||||
|
pass = 1;
|
||||||
|
memcpy(test_array, test_case, sizeof(int) * n);
|
||||||
|
fprintf(stdout, "\tselection sort: ");
|
||||||
|
fflush(stdout);
|
||||||
|
selection_sort(test_array, n);
|
||||||
|
for (i = 0; i < n; i++) {
|
||||||
|
if (test_array[i] != qarray[i]) {
|
||||||
|
fprintf(stdout, "fail\n");
|
||||||
|
failed++;
|
||||||
|
pass = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (pass) {
|
if (pass) {
|
||||||
fprintf(stdout, "pass\n");
|
fprintf(stdout, "pass\n");
|
||||||
passed++;
|
passed++;
|
||||||
|
Loading…
Reference in New Issue
Block a user