diff options
-rw-r--r-- | 16.c | 18 |
1 files changed, 15 insertions, 3 deletions
@@ -26,11 +26,24 @@ qswap(void * base, size_t size, int i, int j) } +void +qreverse(void * base, size_t nmemb, size_t size) +{ + size_t i, j; + + if (nmemb < 2) + return; + + for (i = 0, j = nmemb - 1; i < j; ++i, --j) + qswap(base, size, i, j); +} + + int qpermute(void * base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)) { - size_t pivot, next = 0, i, j; + size_t pivot, next = 0, i; if (nmemb < 2) // No other permutations! @@ -64,8 +77,7 @@ qpermute(void * base, size_t nmemb, size_t size, qswap(base, size, pivot, next); // Reverse all the elements after the new pivot - for (i = pivot + 1, j = nmemb - 1; i < j; ++i, --j) - qswap(base, size, i, j); + qreverse(base + (pivot + 1) * size, nmemb - (pivot + 1), size); return 1; } |