3
#include <stdlib.h> /* malloc, free */
4
#include <string.h> /* memmove */
8
static void merge_part(char * p1, char * p1_end, /* source 1 */
9
char * p2, char * p2_end, /* source 2 */
10
char * s, /* destination */
11
int unit, /* size of each item */
12
int (*f)()) /* comparison routine */
15
{ if (p1 >= p1_end) { memmove(s, p2, p2_end - p2); return; }
16
if (p2 >= p2_end) { memmove(s, p1, p1_end - p1); return; }
18
{ memmove(s, p1, unit);
21
{ memmove(s, p2, unit);
28
static void merge_all(char * p, char * p_end, /* source */
29
char * s, /* destination */
30
int width, /* size of blocks to merge */
31
int unit, /* size of each item */
32
int (*f)()) /* comparison routine */
34
{ char * p2 = p + width;
35
char * p3 = p2 + width;
38
if (p2 > p_end) p2 = p_end;
40
merge_part(p, p2, p2, p3, s, unit, f);
41
if (p3 == p_end) return;
42
p = p3; s += width + width;
46
extern void sort(void * p, void * p_end, int unit, int (*f)())
47
{ int size = (char *) p_end - (char *) p;
48
char * s = (char *) MALLOC(size);
49
char * s_end = s + size;
52
{ merge_all((char *) p, (char *) p_end, s, width, unit, f); width += width;
53
if (width >= size) { memmove(p, s, size); break; }
54
merge_all(s, s_end, (char *) p, width, unit, f); width += width;
55
if (width >= size) break;