1 /* msort -- an alternative to qsort, with an identical interface.
2 Copyright (C) 1992, 1995 Free Software Foundation, Inc.
3 Written by Mike Haertel, September 1988.
5 This file is part of the GNU C Library.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If
19 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 Cambridge, MA 02139, USA. */
29 DEFUN(msort_with_tmp
, (b
, n
, s
, cmp
, t
),
30 PTR b AND
size_t n AND
size_t s AND __compar_fn_t cmp AND
char *t
)
42 b2
= (char *) b
+ (n1
* s
);
44 msort_with_tmp (b1
, n1
, s
, cmp
, t
);
45 msort_with_tmp (b2
, n2
, s
, cmp
, t
);
49 if (s
== OPSIZ
&& (b1
- (char *) 0) % OPSIZ
== 0)
50 /* We are operating on aligned words. Use direct word stores. */
51 while (n1
> 0 && n2
> 0)
53 if ((*cmp
) (b1
, b2
) <= 0)
56 *((op_t
*) tmp
)++ = *((op_t
*) b1
)++;
61 *((op_t
*) tmp
)++ = *((op_t
*) b2
)++;
65 while (n1
> 0 && n2
> 0)
67 if ((*cmp
) (b1
, b2
) <= 0)
82 memcpy (tmp
, b1
, n1
* s
);
83 memcpy (b
, t
, (n
- n2
) * s
);
87 DEFUN(qsort
, (b
, n
, s
, cmp
),
88 PTR b AND
size_t n AND
size_t s AND __compar_fn_t cmp
)
90 CONST
size_t size
= n
* s
;
93 /* The temporary array is small, so put it on the stack. */
94 msort_with_tmp (b
, n
, s
, cmp
, __alloca (size
));
97 /* It's somewhat large, so malloc it. */
99 char *tmp
= malloc (size
);
102 /* Couldn't get space, so use the slower algorithm
103 that doesn't need a temporary array. */
104 extern void EXFUN(_quicksort
, (PTR __base
,
105 size_t __nmemb
, size_t __size
,
106 __compar_fn_t __compar
));
107 _quicksort (b
, n
, s
, cmp
);
111 msort_with_tmp (b
, n
, s
, cmp
, tmp
);