Go to the documentation of this file.
72 #define KSORT_SWAP(type_t, a, b) { register type_t t=(a); (a)=(b); (b)=t; }
74 #define KSORT_INIT(name, type_t, __sort_lt) \
75 void ks_mergesort_##name(size_t n, type_t array[], type_t temp[]) \
77 type_t *a2[2], *a, *b; \
81 a2[1] = temp? temp : (type_t*)malloc(sizeof(type_t) * n); \
82 for (curr = 0, shift = 0; (1ul<<shift) < n; ++shift) { \
83 a = a2[curr]; b = a2[1-curr]; \
85 type_t *p = b, *i, *eb = a + n; \
86 for (i = a; i < eb; i += 2) { \
87 if (i == eb - 1) *p++ = *i; \
89 if (__sort_lt(*(i+1), *i)) { \
90 *p++ = *(i+1); *p++ = *i; \
92 *p++ = *i; *p++ = *(i+1); \
97 size_t i, step = 1ul<<shift; \
98 for (i = 0; i < n; i += step<<1) { \
99 type_t *p, *j, *k, *ea, *eb; \
100 if (n < i + step) { \
101 ea = a + n; eb = a; \
104 eb = a + (n < i + (step<<1)? n : i + (step<<1)); \
106 j = a + i; k = a + i + step; p = b + i; \
107 while (j < ea && k < eb) { \
108 if (__sort_lt(*k, *j)) *p++ = *k++; \
111 while (j < ea) *p++ = *j++; \
112 while (k < eb) *p++ = *k++; \
118 type_t *p = a2[0], *i = a2[1], *eb = array + n; \
119 for (; p < eb; ++i) *p++ = *i; \
121 if (temp == 0) free(a2[1]); \
123 void ks_heapadjust_##name(size_t i, size_t n, type_t l[]) \
127 while ((k = (k << 1) + 1) < n) { \
128 if (k != n - 1 && __sort_lt(l[k], l[k+1])) ++k; \
129 if (__sort_lt(l[k], tmp)) break; \
130 l[i] = l[k]; i = k; \
134 void ks_heapmake_##name(size_t lsize, type_t l[]) \
137 for (i = (lsize >> 1) - 1; i != (size_t)(-1); --i) \
138 ks_heapadjust_##name(i, lsize, l); \
140 void ks_heapsort_##name(size_t lsize, type_t l[]) \
143 for (i = lsize - 1; i > 0; --i) { \
145 tmp = *l; *l = l[i]; l[i] = tmp; ks_heapadjust_##name(0, i, l); \
148 static inline void __ks_insertsort_##name(type_t *s, type_t *t) \
150 type_t *i, *j, swap_tmp; \
151 for (i = s + 1; i < t; ++i) \
152 for (j = i; j > s && __sort_lt(*j, *(j-1)); --j) { \
153 swap_tmp = *j; *j = *(j-1); *(j-1) = swap_tmp; \
156 void ks_combsort_##name(size_t n, type_t a[]) \
158 const double shrink_factor = 1.2473309501039786540366528676643; \
161 type_t tmp, *i, *j; \
164 gap = (size_t)(gap / shrink_factor); \
165 if (gap == 9 || gap == 10) gap = 11; \
168 for (i = a; i < a + n - gap; ++i) { \
170 if (__sort_lt(*j, *i)) { \
171 tmp = *i; *i = *j; *j = tmp; \
175 } while (do_swap || gap > 2); \
176 if (gap != 1) __ks_insertsort_##name(a, a + n); \
178 void ks_introsort_##name(size_t n, type_t a[]) \
181 ks_isort_stack_t *top, *stack; \
182 type_t rp, swap_tmp; \
183 type_t *s, *t, *i, *j, *k; \
187 if (__sort_lt(a[1], a[0])) { swap_tmp = a[0]; a[0] = a[1]; a[1] = swap_tmp; } \
190 for (d = 2; 1ul<<d < n; ++d); \
191 stack = (ks_isort_stack_t*)malloc(sizeof(ks_isort_stack_t) * ((sizeof(size_t)*d)+2)); \
192 top = stack; s = a; t = a + (n-1); d <<= 1; \
196 ks_combsort_##name(t - s + 1, s); \
200 i = s; j = t; k = i + ((j-i)>>1) + 1; \
201 if (__sort_lt(*k, *i)) { \
202 if (__sort_lt(*k, *j)) k = j; \
203 } else k = __sort_lt(*j, *i)? i : j; \
205 if (k != t) { swap_tmp = *k; *k = *t; *t = swap_tmp; } \
207 do ++i; while (__sort_lt(*i, rp)); \
208 do --j; while (i <= j && __sort_lt(rp, *j)); \
210 swap_tmp = *i; *i = *j; *j = swap_tmp; \
212 swap_tmp = *i; *i = *t; *t = swap_tmp; \
214 if (i-s > 16) { top->left = s; top->right = i-1; top->depth = d; ++top; } \
215 s = t-i > 16? i+1 : t; \
217 if (t-i > 16) { top->left = i+1; top->right = t; top->depth = d; ++top; } \
218 t = i-s > 16? i-1 : s; \
221 if (top == stack) { \
223 __ks_insertsort_##name(a, a+n); \
225 } else { --top; s = (type_t*)top->left; t = (type_t*)top->right; d = top->depth; } \
231 type_t ks_ksmall_##name(size_t n, type_t arr[], size_t kk) \
233 type_t *low, *high, *k, *ll, *hh, *mid; \
234 low = arr; high = arr + n - 1; k = arr + kk; \
236 if (high <= low) return *k; \
237 if (high == low + 1) { \
238 if (__sort_lt(*high, *low)) KSORT_SWAP(type_t, *low, *high); \
241 mid = low + (high - low) / 2; \
242 if (__sort_lt(*high, *mid)) KSORT_SWAP(type_t, *mid, *high); \
243 if (__sort_lt(*high, *low)) KSORT_SWAP(type_t, *low, *high); \
244 if (__sort_lt(*low, *mid)) KSORT_SWAP(type_t, *mid, *low); \
245 KSORT_SWAP(type_t, *mid, *(low+1)); \
246 ll = low + 1; hh = high; \
248 do ++ll; while (__sort_lt(*ll, *low)); \
249 do --hh; while (__sort_lt(*low, *hh)); \
250 if (hh < ll) break; \
251 KSORT_SWAP(type_t, *ll, *hh); \
253 KSORT_SWAP(type_t, *low, *hh); \
254 if (hh <= k) low = ll; \
255 if (hh >= k) high = hh - 1; \
258 void ks_shuffle_##name(size_t n, type_t a[]) \
261 for (i = n; i > 1; --i) { \
263 j = (int)(drand48() * i); \
264 tmp = a[j]; a[j] = a[i-1]; a[i-1] = tmp; \
268 #define ks_mergesort(name, n, a, t) ks_mergesort_##name(n, a, t)
269 #define ks_introsort(name, n, a) ks_introsort_##name(n, a)
270 #define ks_combsort(name, n, a) ks_combsort_##name(n, a)
271 #define ks_heapsort(name, n, a) ks_heapsort_##name(n, a)
272 #define ks_heapmake(name, n, a) ks_heapmake_##name(n, a)
273 #define ks_heapadjust(name, i, n, a) ks_heapadjust_##name(i, n, a)
274 #define ks_ksmall(name, n, a, k) ks_ksmall_##name(n, a, k)
275 #define ks_shuffle(name, n, a) ks_shuffle_##name(n, a)
277 #define ks_lt_generic(a, b) ((a) < (b))
278 #define ks_lt_str(a, b) (strcmp((a), (b)) < 0)
282 #define KSORT_INIT_GENERIC(type_t) KSORT_INIT(type_t, type_t, ks_lt_generic)
283 #define KSORT_INIT_STR KSORT_INIT(str, ksstr_t, ks_lt_str)