| 1 |
|
| /* |
| 2 |
|
| =head1 NAME |
| 3 |
|
| |
| 4 |
|
| mergesort_algo.c - an implementation of the mergesort algorithm |
| 5 |
|
| |
| 6 |
|
| =head1 SYNOPSIS |
| 7 |
|
| |
| 8 |
|
| typedef struct my_structure ms_elt_t; |
| 9 |
|
| #define ms_gt(a, b) ((a).comparison_key > (b).comparison_key) |
| 10 |
|
| #define ms_cp(dest, src) ((dest) = (src)) |
| 11 |
|
| #include "mergesort_algo.c" |
| 12 |
|
| |
| 13 |
|
| ... |
| 14 |
|
| |
| 15 |
|
| ms_do_mergesort(...); |
| 16 |
|
| |
| 17 |
|
| =head1 DESCRIPTION |
| 18 |
|
| |
| 19 |
|
| An implementation of the mergesort algorithm, generic with respect to the |
| 20 |
|
| type of items being sorted. |
| 21 |
|
| |
| 22 |
|
| =head1 DEFINITIONS REQUIRED |
| 23 |
|
| |
| 24 |
|
| Before including this file, you must define the following: |
| 25 |
|
| |
| 26 |
|
| =over |
| 27 |
|
| |
| 28 |
|
| =item ms_elt_t |
| 29 |
|
| |
| 30 |
|
| The type for the elements to be sorted. ms_do_mergesort() will operate |
| 31 |
|
| on vectors of these. |
| 32 |
|
| |
| 33 |
|
| =item ms_gt(ms_elt_t B<a>, ms_elt_t B<b>) |
| 34 |
|
| |
| 35 |
|
| A function or macro to compare two ms_elt_t values, and return true if |
| 36 |
|
| B<a> is greater than B<b>. |
| 37 |
|
| |
| 38 |
|
| If implemented as a macro, it may not evaluate either argument more than |
| 39 |
|
| once. |
| 40 |
|
| |
| 41 |
|
| =item ms_cp(ms_elt_t B<dest>, ms_elt_t B<src>) |
| 42 |
|
| |
| 43 |
|
| A function or macro to copy one ms_elt_t value to another. |
| 44 |
|
| |
| 45 |
|
| If implemented as a macro, it may not evaluate either argument more than |
| 46 |
|
| once. |
| 47 |
|
| |
| 48 |
|
| =back |
| 49 |
|
| |
| 50 |
|
| =head1 FUNCTIONS PROVIDED |
| 51 |
|
| |
| 52 |
|
| =over |
| 53 |
|
| |
| 54 |
|
| =item static void ms_do_mergesort(ms_elt_t *B<vec>, size_t B<len>)> |
| 55 |
|
| |
| 56 |
|
| Sorts the elements B<vec>[0] to B<vec>[B<len>-1]. |
| 57 |
|
| |
| 58 |
|
| The sorted elements will be placed in B<vec>[-(B<len>-1) .. 0]. The contents |
| 59 |
|
| of B<vec>[1] to B<vec>[B<len>-1] are undefined after the sort. |
| 60 |
|
| |
| 61 |
|
| Will not attempt to read or write below B<vec>[-(B<len>-1)] or above |
| 62 |
|
| B<vec>[B<len>-1]. |
| 63 |
|
| |
| 64 |
|
| =cut |
| 65 |
|
| */ |
| 66 |
|
| |
| 67 |
|
| static void ms_do_mergesort_2(ms_elt_t *vec); |
| 68 |
|
| static void ms_do_mergesort_3(ms_elt_t *vec); |
| 69 |
|
| |
| 70 |
|
| static void ms__merge_sorted_vectors(ms_elt_t* lower, size_t lower_len, |
| 71 |
|
| ms_elt_t* upper, ms_elt_t* dest); |
| 72 |
|
| |
| 73 |
|
| static void |
| 74 |
9346193 |
| ms_do_mergesort(ms_elt_t *vec, size_t len) |
| 75 |
|
| { |
| 76 |
9346193 |
| switch (len) { |
| 77 |
4 |
| case 0: |
| 78 |
|
| case 1: |
| 79 |
4 |
| break; |
| 80 |
4025837 |
| case 2: |
| 81 |
4025837 |
| ms_do_mergesort_2(vec); |
| 82 |
4025837 |
| break; |
| 83 |
859602 |
| case 3: |
| 84 |
859602 |
| ms_do_mergesort_3(vec); |
| 85 |
859602 |
| break; |
| 86 |
4460750 |
| default: { |
| 87 |
|
| int lower_len, upper_len; |
| 88 |
|
| |
| 89 |
|
| /* Split in half, with the extra elt in the lower part if |
| 90 |
|
| * len is odd */ |
| 91 |
4460750 |
| upper_len = len/2; |
| 92 |
4460750 |
| lower_len = len - upper_len; |
| 93 |
|
| |
| 94 |
|
| /* Sort each half */ |
| 95 |
4460750 |
| ms_do_mergesort(vec, lower_len); |
| 96 |
4460750 |
| ms_do_mergesort(vec+lower_len, upper_len); |
| 97 |
|
| |
| 98 |
|
| /* Merge the sorted halves to vec[len-1 .. 0] */ |
| 99 |
4460750 |
| ms__merge_sorted_vectors( |
| 100 |
4460750 |
| vec-(lower_len-1), lower_len, |
| 101 |
4460750 |
| vec+lower_len - (upper_len-1), |
| 102 |
4460750 |
| vec - (len-1) |
| 103 |
|
| ); |
| 104 |
|
| } |
| 105 |
|
| } |
| 106 |
9346193 |
| } |
| 107 |
|
| |
| 108 |
|
| /* |
| 109 |
|
| =item C<static void ms_do_mergesort_2(ms_elt_t *vec)> |
| 110 |
|
| |
| 111 |
|
| As ms_do_mergesort(), but for a fixed len of 2. |
| 112 |
|
| |
| 113 |
|
| =cut |
| 114 |
|
| */ |
| 115 |
|
| |
| 116 |
|
| static void |
| 117 |
4885439 |
| ms_do_mergesort_2(ms_elt_t *vec) |
| 118 |
|
| { |
| 119 |
|
| /* sort vec[0,1] to vec[-1,0] */ |
| 120 |
|
| |
| 121 |
4885439 |
▶ | if ( ms_gt(vec[0], vec[1]) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ms_gt(vec[0], vec[1]) ) { |
2436696 |
2448743 |
4885439 |
|
| 122 |
2436696 |
| ms_cp(vec[-1], vec[1]); |
| 123 |
|
| /* vec[0] already in place */ |
| 124 |
|
| } else { |
| 125 |
2448743 |
| ms_cp(vec[-1], vec[0]); |
| 126 |
2448743 |
| ms_cp(vec[0], vec[1]); |
| 127 |
|
| } |
| 128 |
4885439 |
| } |
| 129 |
|
| |
| 130 |
|
| /* |
| 131 |
|
| =item C<static void ms_do_mergesort_3(ms_elt_t *vec)> |
| 132 |
|
| |
| 133 |
|
| As ms_do_mergesort(), but for a fixed len of 3. |
| 134 |
|
| |
| 135 |
|
| =cut |
| 136 |
|
| */ |
| 137 |
|
| |
| 138 |
|
| static void |
| 139 |
859602 |
| ms_do_mergesort_3(ms_elt_t *vec) |
| 140 |
|
| { |
| 141 |
|
| /* sort vec[0,1] to vec[-1,0] */ |
| 142 |
859602 |
| ms_do_mergesort_2(vec); |
| 143 |
|
| |
| 144 |
|
| /* merge vec[-1,0] and vec[2] to vec [-2,-1,0] */ |
| 145 |
859602 |
▶ | if ( ms_gt(vec[-1], vec[2]) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ms_gt(vec[-1], vec[2]) ) { |
296338 |
563264 |
859602 |
|
| 146 |
296338 |
| ms_cp(vec[-2], vec[2]); |
| 147 |
|
| /* vec[-1,0] already in place */ |
| 148 |
|
| } else { |
| 149 |
563264 |
| ms_cp(vec[-2], vec[-1]); |
| 150 |
563264 |
▶ | if ( ms_gt(vec[0], vec[2]) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ms_gt(vec[0], vec[2]) ) { |
258957 |
304307 |
563264 |
|
| 151 |
258957 |
| ms_cp(vec[-1], vec[2]); |
| 152 |
|
| /* vec[0] already in place */ |
| 153 |
|
| } else { |
| 154 |
304307 |
| ms_cp(vec[-1], vec[0]); |
| 155 |
304307 |
| ms_cp(vec[0], vec[2]); |
| 156 |
|
| } |
| 157 |
|
| } |
| 158 |
859602 |
| } |
| 159 |
|
| |
| 160 |
|
| /* |
| 161 |
|
| =back |
| 162 |
|
| |
| 163 |
|
| =head1 ALGORITHM |
| 164 |
|
| |
| 165 |
|
| The mergesort algorithm works by splitting the input into two halves, |
| 166 |
|
| recursing into each half and then merging the sorted halves together. |
| 167 |
|
| |
| 168 |
|
| This implementation sorts and merges within a single vector. To sort |
| 169 |
|
| N elements, the elements must be placed in the upper part of a vector |
| 170 |
|
| of length 2N-1. They will be sorted into the lower part. |
| 171 |
|
| |
| 172 |
|
| Consider a sort of 7 elements, initially in reverse order and loaded |
| 173 |
|
| into the upper part of a vector of length 13: |
| 174 |
|
| |
| 175 |
|
| Index 0 1 2 3 4 5 6 7 8 9 A B C |
| 176 |
|
| Value l k j i h g f |
| 177 |
|
| |
| 178 |
|
| First we split the 7 elements into 4 and 3. We take 4 in the lower |
| 179 |
|
| part, and sort the lower part first. To sort those 4 elements, we |
| 180 |
|
| split them into 2 sets of 2. The lower 2 (l,k) are now sorted |
| 181 |
|
| from slots 6,7 to slots 5,6: |
| 182 |
|
| |
| 183 |
|
| Index 0 1 2 3 4 5 6 7 8 9 A B C |
| 184 |
|
| Value k l j i h g f |
| 185 |
|
| |
| 186 |
|
| Next we sort (j,i) from slots 8,9 to slots 7,8: |
| 187 |
|
| |
| 188 |
|
| Index 0 1 2 3 4 5 6 7 8 9 A B C |
| 189 |
|
| Value k l i j h g f |
| 190 |
|
| |
| 191 |
|
| To finish the sort of the lower 4, we merge pairs (k,l) and (i,j) into |
| 192 |
|
| slots 3,4,5,6: |
| 193 |
|
| |
| 194 |
|
| Index 0 1 2 3 4 5 6 7 8 9 A B C |
| 195 |
|
| Value i j k l h g f |
| 196 |
|
| |
| 197 |
|
| Now we move on to sorting (h,g,f). Split it into (h,g) and (f), and sort |
| 198 |
|
| (h,g) into slots 9,A: |
| 199 |
|
| |
| 200 |
|
| Index 0 1 2 3 4 5 6 7 8 9 A B C |
| 201 |
|
| Value i j k l g h f |
| 202 |
|
| |
| 203 |
|
| Now merge (g,h) and (f) into slots 8,9,A: |
| 204 |
|
| |
| 205 |
|
| Index 0 1 2 3 4 5 6 7 8 9 A B C |
| 206 |
|
| Value i j k l f g h |
| 207 |
|
| |
| 208 |
|
| Finally merge (i,j,k,l) and (f,g,h) into slots 0,1,2,3,4,5,6: |
| 209 |
|
| |
| 210 |
|
| Index 0 1 2 3 4 5 6 7 8 9 A B C |
| 211 |
|
| Value f g h i j k l |
| 212 |
|
| |
| 213 |
|
| A couple of points to note: |
| 214 |
|
| |
| 215 |
|
| Each sort of N elements moves the sorted elements down exactly N-1 |
| 216 |
|
| places. Thus sorting the lower part first and ensuring that the lower part |
| 217 |
|
| length is >= the upper part length is enough to prevent the sorted parts |
| 218 |
|
| from overlapping. |
| 219 |
|
| |
| 220 |
|
| When merging, the destination for the merge overlaps the lower source. |
| 221 |
|
| This is ok because the write pointer does not catch the lower read pointer |
| 222 |
|
| until the upper source is exhausted, at which point the merge is done and |
| 223 |
|
| the remainder of the upper source does not need to be moved. |
| 224 |
|
| |
| 225 |
|
| This enables a small optimisation in the merge: there is no need to keep |
| 226 |
|
| track of the end of the upper source, since exhaustion of the upper source |
| 227 |
|
| can be tested for by comparing the write pointer with the lower read pointer. |
| 228 |
|
| |
| 229 |
|
| =head1 PRIVATE FUNCTIONS |
| 230 |
|
| |
| 231 |
|
| Functions with names starting in C<ms__> are not intended for use outside |
| 232 |
|
| this file. |
| 233 |
|
| |
| 234 |
|
| =over |
| 235 |
|
| |
| 236 |
|
| =item ms__merge_sorted_vectors(B<lower>, B<lower_len>, B<upper>, B<dest>) |
| 237 |
|
| |
| 238 |
|
| The merge part of the mergesort algorithm. |
| 239 |
|
| |
| 240 |
|
| B<lower> is a pointer to the lower input vector, B<lower_len> is its length. |
| 241 |
|
| |
| 242 |
|
| B<upper> is a pointer to the upper input vector. |
| 243 |
|
| |
| 244 |
|
| B<dest> is the destination for the merged vector. The destination must be |
| 245 |
|
| such that the dest pointer catches the lower input pointer when the upper |
| 246 |
|
| input vector is exhausted, see ALGORITHM above. |
| 247 |
|
| |
| 248 |
|
| Both input vectors must contain at least 1 element. |
| 249 |
|
| |
| 250 |
|
| =cut |
| 251 |
|
| */ |
| 252 |
|
| |
| 253 |
|
| static void |
| 254 |
4460750 |
| ms__merge_sorted_vectors(ms_elt_t* lower, size_t lower_len, |
| 255 |
|
| ms_elt_t* upper, ms_elt_t* dest) |
| 256 |
|
| { |
| 257 |
4460750 |
| ms_elt_t *lower_end = lower + lower_len; |
| 258 |
|
| |
| 259 |
|
| for (;;) { |
| 260 |
63580735 |
▶ | if ( ms_gt(*lower, *upper) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ms_gt(*lower, *upper) ) { |
31397422 |
32183313 |
63580735 |
|
| 261 |
31397422 |
| ms_cp(*dest++, *upper++); |
| 262 |
31397422 |
▶ | if ( dest >= lower ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( dest >= lower ) { |
2282317 |
29115105 |
31397422 |
|
| 263 |
|
| /* Upper half exhausted - done */ |
| 264 |
2282317 |
| break; |
| 265 |
|
| } |
| 266 |
|
| } else { |
| 267 |
32183313 |
| ms_cp(*dest++, *lower++); |
| 268 |
32183313 |
▶ | if ( lower >= lower_end ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( lower >= lower_end ) { |
2178433 |
30004880 |
32183313 |
|
| 269 |
|
| /* Lower half exhausted */ |
| 270 |
11806006 |
▶ | while (dest < lower) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (dest < lower) { |
9627573 |
2178433 |
11806006 |
|
| 271 |
9627573 |
| ms_cp(*dest++, *upper++); |
| 272 |
|
| } |
| 273 |
2178433 |
| break; |
| 274 |
|
| } |
| 275 |
|
| } |
| 276 |
|
| } |
| 277 |
4460750 |
| } |
| 278 |
|
| |
| 279 |
|
| /* |
| 280 |
|
| =back |
| 281 |
|
| |
| 282 |
|
| =head1 AUTHOR |
| 283 |
|
| |
| 284 |
|
| Nick Cleaton, E<lt>nick@cleaton.netE<gt> |
| 285 |
|
| |
| 286 |
|
| =head1 COPYRIGHT AND LICENSE |
| 287 |
|
| |
| 288 |
|
| Copyright (C) 2010 by Nick Cleaton |
| 289 |
|
| |
| 290 |
|
| This library is free software; you can redistribute it and/or modify |
| 291 |
|
| it under the same terms as Perl itself, either Perl version 5.10.0 or, |
| 292 |
|
| at your option, any later version of Perl 5 you may have available. |
| 293 |
|
| |
| 294 |
|
| =cut |
| 295 |
|
| */ |