Browse Source

add count sort

tags/1.0.0
Chris Cromer 11 months ago
parent
commit
601b3c95ca
Signed by: Chris Cromer <chris@cromer.cl> GPG Key ID: 39CC813FF3C8708A
6 changed files with 100 additions and 7 deletions
  1. 1
    1
      Makefile
  2. 49
    0
      src/count_sort.c
  3. 19
    0
      src/include/count_sort.h
  4. 6
    1
      src/sort.c
  5. 1
    1
      test/Makefile
  6. 24
    4
      test/test.c

+ 1
- 1
Makefile View File

@@ -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=src/sort.c src/random.c src/bubble_sort.c src/timer.c src/count_sort.c
OBJ=$(SRC:.c=.o)

all: sort informe

+ 49
- 0
src/count_sort.c View File

@@ -0,0 +1,49 @@
/*
* 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 <stdlib.h>
#include <string.h>

#include <stdio.h>

/**
* Usar el algoritmo de ordenameniento por conteo
* @param array El array a ordenar
* @param n El tamaño del array
*/
void count_sort(int *array, int n) {
int i;
int j;
int *temp = malloc(sizeof(int) * n);
int *count = malloc(sizeof(int) * n);
memcpy(temp, array, sizeof(int) * n);
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (temp[i] < temp[j]) {
count[j]++;
}
else {
count[i]++;
}
}
}

for (i = 0; i < n; i++) {
array[count[i]] = temp[i];
}

free(count);
free(temp);
}

+ 19
- 0
src/include/count_sort.h View 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_COUNT
#define _SORT_COUNT
void count_sort(int *array, int n);
#endif

+ 6
- 1
src/sort.c View File

@@ -19,6 +19,7 @@
#include "random.h"
#include "timer.h"
#include "bubble_sort.h"
#include "count_sort.h"

#define SORT_VERSION "1.0.0"

@@ -215,7 +216,11 @@ int main (int argc, char **argv) {
// bitonic sort
}
else if (algoritmo == 4) {
// count sort
fprintf(stdout, "Count sort corriendo...\n");
fflush(stdout);
start_timer();
count_sort(array, n);
stop_timer();
}
else if (algoritmo == 5) {
// selection sort

+ 1
- 1
test/Makefile View File

@@ -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
OBJ+=../src/random.o ../src/bubble_sort.o ../src/timer.o ../src/count_sort.o

all: test


+ 24
- 4
test/test.c View File

@@ -19,6 +19,7 @@
#include <unistd.h>
#include "random.h"
#include "bubble_sort.h"
#include "count_sort.h"

static int *test_case;
static int *test_array;
@@ -50,7 +51,7 @@ int main(int argc, char **argv) {
int n = 50000;
int i;
int gen;
int pass = 1;
int pass;
int passed = 0;
int failed = 0;

@@ -63,6 +64,7 @@ int main(int argc, char **argv) {
fprintf(stdout, "Running tests:\n");

// Test random number generation
pass = 1;
fprintf(stdout, "\trandom generation: ");
fflush(stdout);
for (i = 0; i < 5000000; i++) {
@@ -78,22 +80,22 @@ int main(int argc, char **argv) {
fprintf(stdout, "pass\n");
passed++;
}
pass = 1;

// Prepare for sort tests
for (i = 0; i < n; i++) {
test_case[i] = gen_rand(-100, 100);
}
memcpy(qarray, test_case, sizeof(int) * n);
memcpy(test_array, test_case, sizeof(int) * n);
qsort(qarray, n, sizeof(int), compar);

// Test bubble sort
pass = 1;
memcpy(test_array, test_case, sizeof(int) * n);
fprintf(stdout, "\tbubble sort: ");
fflush(stdout);
bubble_sort(test_array, n);
for (i = 0; i < n; i++) {
if (test_array[i] != qarray[i] && pass) {
if (test_array[i] != qarray[i]) {
fprintf(stdout, "fail\n");
failed++;
pass = 0;
@@ -104,7 +106,25 @@ int main(int argc, char **argv) {
fprintf(stdout, "pass\n");
passed++;
}

// Test count sort
pass = 1;
memcpy(test_array, test_case, sizeof(int) * n);
fprintf(stdout, "\tcount sort: ");
fflush(stdout);
count_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);

Loading…
Cancel
Save