merge changes

Este commit está contenido en:
Chris Cromer 2018-11-13 12:20:01 -03:00
commit f2763bf41f
Firmado por: cromer
ID de clave GPG: 39CC813FF3C8708A
Se han modificado 6 ficheros con 83 adiciones y 3 borrados

Ver fichero

@ -1,7 +1,7 @@
CC=gcc
CFLAGS=-Wall -Isrc/include -DDEBUG -g
#LDFLAGS=-lm
SRC=src/sort.c src/random.c src/bubble_sort.c src/timer.c src/count_sort.c
SRC=src/sort.c src/random.c src/bubble_sort.c src/timer.c src/count_sort.c src/quick_sort.c
OBJ=$(SRC:.c=.o)
all: sort informe

19
src/include/quick_sort.h Archivo normal
Ver fichero

@ -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_QUICK
#define _SORT_QUICK
void quick_sort(int *array, int n);
#endif

34
src/quick_sort.c Archivo normal
Ver fichero

@ -0,0 +1,34 @@
/*
* 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 quick_sort(int *array, int n) {
if(n<2)return;
int i;
int j;
int temp;
int pivote=array[n/2];
for(i=0,j=n-1;;i++,j--){
while(array[i] < pivote) i++;
while(array[j] > pivote) j--;
if(i>=j)break;
temp=array[i];
array[i] = array[j];
array[j] = temp;
}
quick_sort(array,i);
quick_sort(array+i,n-i);
}

Ver fichero

@ -21,6 +21,7 @@
#include "timer.h"
#include "bubble_sort.h"
#include "count_sort.h"
#include "quick_sort.h"
#define SORT_VERSION "1.0.0"
@ -194,7 +195,13 @@ int main (int argc, char **argv) {
}
if (quick) {
// quick sort
fprintf(stdout, "Quick sort corriendo...\n");
fflush(stdout);
memcpy(work_array, unordered_array, sizeof(int) * n);
start_timer();
quick_sort(work_array, n);
stop_timer();
print_timer();
}
if (bubble) {

Ver fichero

@ -2,7 +2,7 @@ CC=gcc
CFLAGS=-Wall -I../src/include -DDEBUG -g
SRC=test.c
OBJ=$(SRC:.c=.o)
OBJ+=../src/random.o ../src/bubble_sort.o ../src/timer.o ../src/count_sort.o
OBJ+=../src/random.o ../src/bubble_sort.o ../src/timer.o ../src/count_sort.o ../src/quick_sort.o
all: test

Ver fichero

@ -20,6 +20,7 @@
#include "random.h"
#include "bubble_sort.h"
#include "count_sort.h"
#include "quick_sort.h"
static int *test_case;
static int *test_array;
@ -126,6 +127,25 @@ int main(int argc, char **argv) {
passed++;
}
// Test quick sort
pass = 1;
memcpy(test_array, test_case, sizeof(int) * n);
fprintf(stdout, "\tquick sort: ");
fflush(stdout);
quick_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++;
}
fprintf(stdout, "%d tests passed\n", passed);
fprintf(stdout, "%d tests failed\n", failed);