Download code

Jump to: navigation, search

Back to Heapsort_(C)

Download for Windows: zip

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

heapsort.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/Heapsort_(C)?oldid=19139
14 */
15 
16 #ifndef _HEAPSORT_H_
17 
18 #include <stddef.h>
19 
20 void heapsort(void *base, size_t num_elements, size_t element_size,
21               int(*comparer)(const void *, const void *))
22 ;
23 
24 #endif
25 


hijacker
hijacker
hijacker
hijacker

heapsort.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/Heapsort_(C)?oldid=19139
14 */
15 
16 #include "heapsort.h"
17 
18 #define HEAP_ROOT       0
19 #define LEFT_CHILD(i)   (2*(i) + 1)
20 #define RIGHT_CHILD(i)  (2*(i) + 2)
21 #define PARENT(i)       ((i-1) / 2)
22 static int compare_elements_helper(void *base, size_t element_size, int idx1, int idx2,
23                                    int(*comparer)(const void *, const void*))
24 {
25     char* base_bytes = base;
26     return comparer(&base_bytes[idx1*element_size], &base_bytes[idx2*element_size]);
27 }
28 
29 #define element_less_than(i,j)  (compare_elements_helper(base, element_size, (i), (j), comparer) < 0)
30 #define element_greater_or_equal(i,j)  (compare_elements_helper(base, element_size, (i), (j), comparer) >= 0)
31 
32 static void exchange_elements_helper(void *base, size_t element_size, int idx1, int idx2)
33 {
34     char* base_bytes = base;
35     int i;
36     for (i=0; i<element_size; i++)
37     {
38         char temp = base_bytes[idx1*element_size + i];
39         base_bytes[idx1*element_size + i] = base_bytes[idx2*element_size + i];
40         base_bytes[idx2*element_size + i] = temp;
41     }
42 }
43 
44 #define exchange_elements(i,j)  (exchange_elements_helper(base, element_size, (i), (j)))
45 void heapsort(void *base, size_t num_elements, size_t element_size,
46               int(*comparer)(const void *, const void *))
47 
48 {
49     int heap_size = num_elements;
50     int i;
51     for (i=0; i<num_elements; i++)
52     {
53         /* Bubble up element i */
54         while (!element_greater_or_equal(PARENT(i), i))
55         {
56             exchange_elements(PARENT(i), i);
57             i = PARENT(i);
58         }
59     }
60     while (heap_size > 0)
61     {
62         exchange_elements(HEAP_ROOT, heap_size - 1);
63         heap_size--;
64 
65         /* Bubble down the root */
66 	i = 0;
67 	while (1)
68 	{
69 	    int has_left_child  = LEFT_CHILD(i)  < heap_size;
70 	    int has_right_child = RIGHT_CHILD(i) < heap_size;
71 	    if (has_left_child && !element_greater_or_equal(i, LEFT_CHILD(i)) &&
72 	        (!has_right_child || element_greater_or_equal(LEFT_CHILD(i), RIGHT_CHILD(i))))
73 	    {
74 	        exchange_elements(i, LEFT_CHILD(i));
75 	        i = LEFT_CHILD(i);
76 	    }
77 	    else if (has_right_child && !element_greater_or_equal(i, RIGHT_CHILD(i)))
78 	    {
79 	        exchange_elements(i, RIGHT_CHILD(i));
80 	        i = RIGHT_CHILD(i);
81 	    }
82 	    else
83 	    {
84 	        break;
85 	    }
86 	}
87     }
88 }


hijacker
hijacker
hijacker
hijacker