1 /* Stable-sorting of an array using mergesort.
2 Copyright (C) 2009-2020 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2009.
5 This program is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* This file implements stable sorting of an array, using the mergesort
20 Worst-case running time for an array of length N is O(N log N).
21 Unlike the mpsort module, the algorithm here attempts to minimize not
22 only the number of comparisons, but also the number of copying operations.
24 Before including this file, you need to define
25 ELEMENT The type of every array element.
26 COMPARE A two-argument macro that takes two 'const ELEMENT *'
27 pointers and returns a negative, zero, or positive 'int'
28 value if the element pointed to by the first argument is,
29 respectively, less, equal, or greater than the element
30 pointed to by the second argument.
31 STATIC The storage class of the functions being defined.
32 STATIC_FROMTO (Optional.) Overrides STATIC for the 'merge_sort_fromto'
34 Before including this file, you also need to include:
38 /* Merge the sorted arrays src1[0..n1-1] and src2[0..n2-1] into
39 dst[0..n1+n2-1]. In case of ambiguity, put the elements of src1
40 before the elements of src2.
41 n1 and n2 must be > 0.
42 The arrays src1 and src2 must not overlap the dst array, except that
43 src1 may be dst[n2..n1+n2-1], or src2 may be dst[n1..n1+n2-1]. */
45 merge (const ELEMENT
*src1
, size_t n1
,
46 const ELEMENT
*src2
, size_t n2
,
49 for (;;) /* while (n1 > 0 && n2 > 0) */
51 if (COMPARE (src1
, src2
) <= 0)
66 /* Here n1 == 0 || n2 == 0 but also n1 > 0 || n2 > 0. */
89 /* Sort src[0..n-1] into dst[0..n-1], using tmp[0..n/2-1] as temporary
91 The arrays src, dst, tmp must not overlap. */
98 merge_sort_fromto (const ELEMENT
*src
, ELEMENT
*dst
, size_t n
, ELEMENT
*tmp
)
110 if (COMPARE (&src
[0], &src
[1]) <= 0)
112 /* src[0] <= src[1] */
124 if (COMPARE (&src
[0], &src
[1]) <= 0)
126 if (COMPARE (&src
[1], &src
[2]) <= 0)
128 /* src[0] <= src[1] <= src[2] */
133 else if (COMPARE (&src
[0], &src
[2]) <= 0)
135 /* src[0] <= src[2] < src[1] */
142 /* src[2] < src[0] <= src[1] */
150 if (COMPARE (&src
[0], &src
[2]) <= 0)
152 /* src[1] < src[0] <= src[2] */
157 else if (COMPARE (&src
[1], &src
[2]) <= 0)
159 /* src[1] <= src[2] < src[0] */
166 /* src[2] < src[1] < src[0] */
176 size_t n2
= (n
+ 1) / 2;
177 /* Note: n1 + n2 = n, n1 <= n2. */
178 /* Sort src[n1..n-1] into dst[n1..n-1], scratching tmp[0..n2/2-1]. */
179 merge_sort_fromto (src
+ n1
, dst
+ n1
, n2
, tmp
);
180 /* Sort src[0..n1-1] into tmp[0..n1-1], scratching dst[0..n1-1]. */
181 merge_sort_fromto (src
, tmp
, n1
, dst
);
182 /* Merge the two half results. */
183 merge (tmp
, n1
, dst
+ n1
, n2
, dst
);
189 /* Sort src[0..n-1], using tmp[0..n-1] as temporary (scratch) storage.
190 The arrays src, tmp must not overlap. */
192 merge_sort_inplace (ELEMENT
*src
, size_t n
, ELEMENT
*tmp
)
202 if (COMPARE (&src
[0], &src
[1]) <= 0)
204 /* src[0] <= src[1] */
215 if (COMPARE (&src
[0], &src
[1]) <= 0)
217 if (COMPARE (&src
[1], &src
[2]) <= 0)
219 /* src[0] <= src[1] <= src[2] */
221 else if (COMPARE (&src
[0], &src
[2]) <= 0)
223 /* src[0] <= src[2] < src[1] */
230 /* src[2] < src[0] <= src[1] */
239 if (COMPARE (&src
[0], &src
[2]) <= 0)
241 /* src[1] < src[0] <= src[2] */
246 else if (COMPARE (&src
[1], &src
[2]) <= 0)
248 /* src[1] <= src[2] < src[0] */
256 /* src[2] < src[1] < src[0] */
266 size_t n2
= (n
+ 1) / 2;
267 /* Note: n1 + n2 = n, n1 <= n2. */
268 /* Sort src[n1..n-1], scratching tmp[0..n2-1]. */
269 merge_sort_inplace (src
+ n1
, n2
, tmp
);
270 /* Sort src[0..n1-1] into tmp[0..n1-1], scratching tmp[n1..2*n1-1]. */
271 merge_sort_fromto (src
, tmp
, n1
, tmp
+ n1
);
272 /* Merge the two half results. */
273 merge (tmp
, n1
, src
+ n1
, n2
, src
);