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 Before including this file, you also need to include:
36 /* Merge the sorted arrays src1[0..n1-1] and src2[0..n2-1] into
37 dst[0..n1+n2-1]. In case of ambiguity, put the elements of src1
38 before the elements of src2.
39 n1 and n2 must be > 0.
40 The arrays src1 and src2 must not overlap the dst array, except that
41 src1 may be dst[n2..n1+n2-1], or src2 may be dst[n1..n1+n2-1]. */
43 merge (const ELEMENT
*src1
, size_t n1
,
44 const ELEMENT
*src2
, size_t n2
,
47 for (;;) /* while (n1 > 0 && n2 > 0) */
49 if (COMPARE (src1
, src2
) <= 0)
64 /* Here n1 == 0 || n2 == 0 but also n1 > 0 || n2 > 0. */
87 /* Sort src[0..n-1] into dst[0..n-1], using tmp[0..n/2-1] as temporary
89 The arrays src, dst, tmp must not overlap. */
91 merge_sort_fromto (const ELEMENT
*src
, ELEMENT
*dst
, size_t n
, ELEMENT
*tmp
)
103 if (COMPARE (&src
[0], &src
[1]) <= 0)
105 /* src[0] <= src[1] */
117 if (COMPARE (&src
[0], &src
[1]) <= 0)
119 if (COMPARE (&src
[1], &src
[2]) <= 0)
121 /* src[0] <= src[1] <= src[2] */
126 else if (COMPARE (&src
[0], &src
[2]) <= 0)
128 /* src[0] <= src[2] < src[1] */
135 /* src[2] < src[0] <= src[1] */
143 if (COMPARE (&src
[0], &src
[2]) <= 0)
145 /* src[1] < src[0] <= src[2] */
150 else if (COMPARE (&src
[1], &src
[2]) <= 0)
152 /* src[1] <= src[2] < src[0] */
159 /* src[2] < src[1] < src[0] */
169 size_t n2
= (n
+ 1) / 2;
170 /* Note: n1 + n2 = n, n1 <= n2. */
171 /* Sort src[n1..n-1] into dst[n1..n-1], scratching tmp[0..n2/2-1]. */
172 merge_sort_fromto (src
+ n1
, dst
+ n1
, n2
, tmp
);
173 /* Sort src[0..n1-1] into tmp[0..n1-1], scratching dst[0..n1-1]. */
174 merge_sort_fromto (src
, tmp
, n1
, dst
);
175 /* Merge the two half results. */
176 merge (tmp
, n1
, dst
+ n1
, n2
, dst
);
182 /* Sort src[0..n-1], using tmp[0..n-1] as temporary (scratch) storage.
183 The arrays src, tmp must not overlap. */
185 merge_sort_inplace (ELEMENT
*src
, size_t n
, ELEMENT
*tmp
)
195 if (COMPARE (&src
[0], &src
[1]) <= 0)
197 /* src[0] <= src[1] */
208 if (COMPARE (&src
[0], &src
[1]) <= 0)
210 if (COMPARE (&src
[1], &src
[2]) <= 0)
212 /* src[0] <= src[1] <= src[2] */
214 else if (COMPARE (&src
[0], &src
[2]) <= 0)
216 /* src[0] <= src[2] < src[1] */
223 /* src[2] < src[0] <= src[1] */
232 if (COMPARE (&src
[0], &src
[2]) <= 0)
234 /* src[1] < src[0] <= src[2] */
239 else if (COMPARE (&src
[1], &src
[2]) <= 0)
241 /* src[1] <= src[2] < src[0] */
249 /* src[2] < src[1] < src[0] */
259 size_t n2
= (n
+ 1) / 2;
260 /* Note: n1 + n2 = n, n1 <= n2. */
261 /* Sort src[n1..n-1], scratching tmp[0..n2-1]. */
262 merge_sort_inplace (src
+ n1
, n2
, tmp
);
263 /* Sort src[0..n1-1] into tmp[0..n1-1], scratching tmp[n1..2*n1-1]. */
264 merge_sort_fromto (src
, tmp
, n1
, tmp
+ n1
);
265 /* Merge the two half results. */
266 merge (tmp
, n1
, src
+ n1
, n2
, src
);