1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include "sorters.h"
#include "common.h"
#define TAIL(base,nmel,width) ((base) + ((nmel) - 1) * (width))
/* get index of last block whose head is less than the previous block's tail */
static size_t last_overlap(char *base, size_t bcount, size_t bwidth, size_t width, cmpfun cmp)
{
for (char *cur = TAIL(base, bcount, bwidth); --bcount; cur -= bwidth)
if (cmp(cur - width, cur) > 0) break;
return bcount;
}
void merge(char *buf, char *base, size_t anmel, size_t bnmel, size_t width, cmpfun cmp)
{
char *a = buf;
char *b = base + anmel * width;
size_t skip = binary_search(b, base, anmel, width, cmp);
anmel -= skip;
base += skip * width;
swap(base, a, anmel * width);
while (anmel && bnmel) {
if (cmp(a, b) <= 0) { swap(base, a, width); a += width; anmel--; }
else { swap(base, b, width); b += width; bnmel--; }
base += width;
}
swap(base, a, anmel * width);
}
void grailsort(void *unsorted, size_t nmel, size_t width, cmpfun cmp)
{
char *base = unsorted;
if (nmel <= MAX_SORTNET) {
sorting_network(base, nmel, width, cmp);
return;
}
size_t blknmel = sqrt(nmel); /* elements in a block */
size_t bufnmel = blknmel + nmel % blknmel; /* elements in the buffer */
size_t bwidth = blknmel * width; /* size of a block in bytes */
size_t blocks = nmel / blknmel - 1; /* number of blocks in a + b */
size_t acount = blocks / 2;
size_t bcount = blocks - acount;
char *a = base + bufnmel * width;
char *b = a + acount * bwidth;
grailsort(a, acount * blknmel, width, cmp);
grailsort(b, bcount * blknmel, width, cmp);
/* if already sorted, nothing to do */
if (cmp(TAIL(a, acount * blknmel, width), b) <= 0)
goto distribute;
/* sort all the a and b blocks together by their head elements */
grailsort(a, blocks, bwidth, cmp);
/* merge, starting from the end and working towards the beginning */
while (blocks > 1) {
size_t overlap = last_overlap(a, blocks, bwidth, width, cmp);
if (overlap > 0) {
merge(base, TAIL(a, overlap, bwidth), blknmel, (blocks - overlap) * blknmel, width, cmp);
}
blocks = overlap;
}
distribute:
grailsort(base, bufnmel, width, cmp);
distribute_buffer(base, bufnmel, nmel - bufnmel, width, cmp);
}
|