summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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;
}