summaryrefslogtreecommitdiff
path: root/grailsort.c
diff options
context:
space:
mode:
authorBobby Bingham <koorogi@koorogi.info>2014-08-16 19:55:46 -0500
committerBobby Bingham <koorogi@koorogi.info>2014-08-16 19:55:46 -0500
commit706da1805c0bad2a4e6f1928b7eda003f02a0ac0 (patch)
tree1c2f89d3e6c06e65069338b1c8b94348dd9080d7 /grailsort.c
parent949dc1f78744da787f21f740118662fb20db3d42 (diff)
Slightly simplify grailsort
Remove unused pending variable, and unused return value from merge(). Saves 29 bytes on x86_64.
Diffstat (limited to 'grailsort.c')
-rw-r--r--grailsort.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/grailsort.c b/grailsort.c
index 093a5a2..ac0bf20 100644
--- a/grailsort.c
+++ b/grailsort.c
@@ -15,13 +15,15 @@ static size_t last_overlap(char *base, size_t bcount, size_t bwidth, size_t widt
return bcount;
}
-size_t merge(char *buf, char *base, size_t anmel, size_t bnmel, size_t width, cmpfun cmp)
+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 sorted;
- for (sorted = 0; anmel && cmp(base, b) <= 0; sorted++, anmel--) base += width;
+ while (anmel && cmp(base, b) <= 0) {
+ anmel -= 1;
+ base += width;
+ }
swap(base, a, anmel * width);
@@ -32,7 +34,6 @@ size_t merge(char *buf, char *base, size_t anmel, size_t bnmel, size_t width, cm
}
swap(base, a, anmel * width);
- return sorted;
}
void grailsort(void *unsorted, size_t nmel, size_t width, cmpfun cmp)
@@ -65,11 +66,11 @@ void grailsort(void *unsorted, size_t nmel, size_t width, cmpfun cmp)
grailsort(a, blocks, bwidth, cmp);
/* merge, starting from the end and working towards the beginning */
- size_t pending = 0;
while (blocks > 1) {
size_t overlap = last_overlap(a, blocks, bwidth, width, cmp);
- if (overlap == 0) break;
- pending = merge(base, TAIL(a, overlap, bwidth), blknmel, (blocks - overlap) * blknmel, width, cmp);
+ if (overlap > 0) {
+ merge(base, TAIL(a, overlap, bwidth), blknmel, (blocks - overlap) * blknmel, width, cmp);
+ }
blocks = overlap;
}