From 706da1805c0bad2a4e6f1928b7eda003f02a0ac0 Mon Sep 17 00:00:00 2001 From: Bobby Bingham Date: Sat, 16 Aug 2014 19:55:46 -0500 Subject: Slightly simplify grailsort Remove unused pending variable, and unused return value from merge(). Saves 29 bytes on x86_64. --- grailsort.c | 15 ++++++++------- 1 file 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; } -- cgit v1.2.3