Download code

Jump to: navigation, search

Back to Selection_sort_(C)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

selection_sort.h

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Selection_sort_(C)?oldid=13212
14 */
15 
16 #ifndef _SELECTION_SORT_H_
17 
18 #include <stddef.h>   /* for size_t */
19 
20 void selection_sort_int_array(int a[], int num_elements);
21 void selection_sort(void * base, size_t num_elements, size_t element_size,
22                     int (*comparer)(const void *, const void *)); 
23 
24 
25 #define less_than(x,y)  ((x) < (y))
26 #define DECLARE_MACRO_SELECTION_SORT(name, T, compare_less) \
27 void name(T a[], int num_elements);
28 
29 #define DEFINE_MACRO_SELECTION_SORT(name, T, compare_less) \
30 void name(T a[], int num_elements) \
31 { \
32     int sorted_size; \
33     for(sorted_size=0; sorted_size < num_elements; sorted_size++) \
34     { \
35         int min_element_index = sorted_size; \
36         T min_element_value = a[sorted_size]; \
37         int i; \
38         for(i=sorted_size+1; i<num_elements; i++) \
39         { \
40             if (compare_less(a[i], min_element_value)) \
41             { \
42                 min_element_index = i; \
43                 min_element_value = a[i]; \
44             } \
45         } \
46         a[min_element_index] = a[sorted_size]; \
47         a[sorted_size] = min_element_value; \
48     } \
49 }
50 
51 #endif
52 


hijacker
hijacker
hijacker
hijacker

selection_sort.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Selection_sort_(C)?oldid=13212
14 */
15 
16 #include "selection_sort.h"
17 
18 static int compare_elements_helper(void *base, size_t element_size, size_t idx1, size_t idx2,
19                                    int(*comparer)(const void *, const void*))
20 {
21     char* base_bytes = (char*)base;
22     return comparer(&base_bytes[idx1*element_size], &base_bytes[idx2*element_size]);
23 }
24 
25 #define element_less_than(i,j)  (compare_elements_helper(base, element_size, (i), (j), comparer) < 0)
26 
27 static void exchange_elements_helper(void *base, size_t element_size, size_t idx1, size_t idx2)
28 {
29     char* base_bytes = (char*)base;
30     int i;
31     for (i=0; i<element_size; i++)
32     {
33         char temp = base_bytes[idx1*element_size + i];
34         base_bytes[idx1*element_size + i] = base_bytes[idx2*element_size + i];
35         base_bytes[idx2*element_size + i] = temp;
36     }
37 }
38 
39 #define exchange_elements(i,j)  (exchange_elements_helper(base, element_size, (i), (j)))
40 
41 void selection_sort_int_array(int a[], int num_elements)
42 {
43     int sorted_size;
44     for(sorted_size=0; sorted_size < num_elements; sorted_size++)
45     {
46         int min_element_index = sorted_size;
47 	int min_element_value= a[sorted_size];
48 	int i;
49 	for(i=sorted_size+1; i<num_elements; i++) 
50 	{
51 	    if (a[i] < min_element_value)
52 	    {
53 	        min_element_index = i;
54 	        min_element_value = a[i];
55 	    }
56 	}
57         a[min_element_index] = a[sorted_size];
58 	a[sorted_size] = min_element_value;
59     }
60 }
61 
62 
63 void selection_sort(void * base, size_t num_elements, size_t element_size,
64                     int (*comparer)(const void *, const void *))
65 {
66     size_t sorted_size;
67     for(sorted_size=0; sorted_size < num_elements; sorted_size++)
68     {
69         size_t min_element_index = sorted_size;
70         size_t i;
71         for(i=sorted_size+1; i<num_elements; i++) 
72         {
73             if (element_less_than(i,min_element_index))
74             {
75                 min_element_index = i;
76             }
77         }
78         exchange_elements(min_element_index, sorted_size);
79     }
80 }


hijacker
hijacker
hijacker
hijacker

selection_sort_test.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Selection_sort_(C)?oldid=13212
14 */
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include "selection_sort.h"
19 
20 DECLARE_MACRO_SELECTION_SORT(selection_sort_int_array2, int, less_than)
21 DEFINE_MACRO_SELECTION_SORT(selection_sort_int_array2, int, less_than)
22 
23 int less_compare(const void * a, const void * b)
24 {
25     return (*(const int*)a) - (*(const int*)b);
26 }
27 
28 int main(int argc, char* argv[])
29 {
30     int length = atoi(argv[1]);
31     int* a = (int*)malloc(sizeof(int) * length);
32     int i;
33 
34     for (i=0; i<length; i++)
35     {
36         a[i] = rand();
37     }
38     selection_sort_int_array(a, length);
39     for (i=1; i<length; i++)
40     {
41         if (a[i] < a[i-1])
42         {
43             puts("ERROR");
44         }
45     }
46 
47     for (i=0; i<length; i++)
48     {
49         a[i] = rand();
50     }
51     selection_sort(a, length, sizeof(int), less_compare);
52     for (i=1; i<length; i++)
53     {
54         if (a[i] < a[i-1])
55         {
56             puts("ERROR");
57         }
58     }
59 
60     for (i=0; i<length; i++)
61     {
62         a[i] = rand();
63     }
64     selection_sort_int_array2(a, length);
65     for (i=1; i<length; i++)
66     {
67         if (a[i] < a[i-1])
68         {
69             puts("ERROR");
70         }
71     }
72 
73     return 0;
74 }


hijacker
hijacker
hijacker
hijacker