selectionsort

This commit is contained in:
Rodolfo Cuevas 2018-11-16 09:41:22 -03:00
parent fa78806865
commit 67aab5f842
4 changed files with 112 additions and 2 deletions

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_SELECTION
#define _SORT_SELECTION
void selection_sort(int *array, int n);
#endif

35
src/selection_sort.c Normal file
View 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]);
}
}

View File

@ -22,6 +22,8 @@
#include "bubble_sort.h"
#include "count_sort.h"
#include "quick_sort.h"
#include "bitonic_sort.h"
#include "selection_sort.h"
#define SORT_VERSION "1.0.0"
@ -243,7 +245,14 @@ int main (int argc, char **argv) {
}
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) {
@ -258,7 +267,14 @@ int main (int argc, char **argv) {
}
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) {

View File

@ -21,6 +21,8 @@
#include "bubble_sort.h"
#include "count_sort.h"
#include "quick_sort.h"
#include "bitonic_sort.h"
#include "selection_sort.h"
static int *test_case;
static int *test_array;
@ -146,6 +148,44 @@ int main(int argc, char **argv) {
passed++;
}
//test bitonic
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) {
fprintf(stdout, "pass\n");
passed++;
}
fprintf(stdout, "%d tests passed\n", passed);
fprintf(stdout, "%d tests failed\n", failed);