3 ms·
> I hadn't thought of writing a tail-recursive dumbsort, though I'm not sure this is really simpler (untested): I meant something more like this: void
by Dylan16807 5y ago
> I hadn't thought of writing a tail-recursive dumbsort, though I'm not sure this is really simpler (untested):
I meant something more like this:
void
dumbsort(int *p, size_t n)
{
int tmp;
for (size_t i = 1; i < n; i++) {
if (p[i] < p[i-1]) {
tmp = p[i];
p[i] = p[i-1];
p[i-1] = tmp;
return dumbsort(p, n);
}
}
The same as the original except it doesn't need to manipulate the loop variable.
In the assembly version, you replace wiping eax with a jump.
- kragen 5y agoOh, of course you're right. In FORTH you'd need an UNLOOP there if you were using a DO LOOP. It's kind of bogus that the 8086 loop-counter register CX isn't one of the index registers, isn't it?