Fix heapsort, add test

This commit is contained in:
Nils O. Selåsdal
2013-12-03 11:30:09 +01:00
parent a5662a2edd
commit 70fb352656
4 changed files with 130 additions and 16 deletions
+15 -11
View File
@@ -3,39 +3,43 @@
static void
uc_sift(unsigned char *base, size_t start, size_t count, size_t width,
uc_hs_cmp cmp)
uc_hs_cmp cmp, uc_hs_swp swap, void *cookie)
{
size_t root = start, child;
while ((root * 2 + 1) < count) {
child = root * 2 + 1;
if (child < (count - 1)
&& cmp(&base[child * width], &base[(child + 1) * width]) < 0)
child++;
if (child < (count - 1)) {
int rc = cmp(&base[child * width], &base[(child + 1) * width], cookie);
if (rc < 0) {
child++;
}
}
if (cmp(&base[root * width], &base[child * width]) < 0) {
memcpy(base + root * width, base + child * width, width);
if (cmp(&base[root * width], &base[child * width], cookie) < 0) {
swap(base + root * width, base + child * width);
root = child;
} else
} else {
return;
}
}
}
void
uc_heapsort(void *base_, size_t count, size_t width,
uc_hs_cmp cmp)
uc_hs_cmp cmp, uc_hs_swp swap, void *cookie)
{
int start = count / 2 - 1, end = count - 1;
unsigned char *base = base_;
while (start >= 0) {
uc_sift(base, start, count, width, cmp);
uc_sift(base, start, count, width, cmp, swap, cookie);
start--;
}
while (end > 0) {
memcpy(base + end * width, base, width);
uc_sift(base, 0, end, width, cmp);
swap(base + end * width, base);
uc_sift(base, 0, end, width, cmp, swap, cookie);
end--;
}
}