mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-05-22 07:09:18 +02:00
fixing off by one error in quicksort
This commit is contained in:
parent
f58670b531
commit
566fdee273
1 changed files with 5 additions and 4 deletions
|
@ -112,6 +112,7 @@ static sexp sexp_object_compare_op (sexp ctx sexp_api_params(self, n), sexp a, s
|
||||||
return sexp_make_fixnum(sexp_object_compare(ctx, a, b));
|
return sexp_make_fixnum(sexp_object_compare(ctx, a, b));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* fast path when using general object-cmp comparator with no key */
|
||||||
static void sexp_qsort (sexp ctx, sexp *vec, sexp_sint_t lo, sexp_sint_t hi) {
|
static void sexp_qsort (sexp ctx, sexp *vec, sexp_sint_t lo, sexp_sint_t hi) {
|
||||||
sexp_sint_t mid, i, j;
|
sexp_sint_t mid, i, j;
|
||||||
sexp tmp, tmp2;
|
sexp tmp, tmp2;
|
||||||
|
@ -125,8 +126,8 @@ static void sexp_qsort (sexp ctx, sexp *vec, sexp_sint_t lo, sexp_sint_t hi) {
|
||||||
swap(tmp, vec[j], vec[hi]);
|
swap(tmp, vec[j], vec[hi]);
|
||||||
if ((hi-lo) > 2) {
|
if ((hi-lo) > 2) {
|
||||||
sexp_qsort(ctx, vec, lo, j-1);
|
sexp_qsort(ctx, vec, lo, j-1);
|
||||||
lo = j+1;
|
lo = j;
|
||||||
goto loop;
|
goto loop; /* tail recurse on right side */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -172,8 +173,8 @@ static sexp sexp_qsort_less (sexp ctx, sexp *vec,
|
||||||
res = sexp_qsort_less(ctx, vec, lo, j-1, less, key);
|
res = sexp_qsort_less(ctx, vec, lo, j-1, less, key);
|
||||||
if (sexp_exceptionp(res))
|
if (sexp_exceptionp(res))
|
||||||
goto done;
|
goto done;
|
||||||
lo = j+1;
|
lo = j;
|
||||||
goto loop;
|
goto loop; /* tail recurse on right side */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
done:
|
done:
|
||||||
|
|
Loading…
Add table
Reference in a new issue