Since the comparison function of the original qsort only accepts two pointers, passing in additional parameters (e.g. producing a comparison function that compares by the two value's difference with another value) must be done using
global variables. The issue was solved by the
BSD and
GNU Unix-like systems by introducing a qsort_r function, which allows for an additional parameter to be passed to the comparison function. The two versions of qsort_r have different argument orders.
C11 Annex K defines a qsort_s essentially identical to GNU's qsort_r. The
macOS and
FreeBSD libcs also contain qsort_b, a variant that uses
blocks, an analogue to
closures, as an alternate solution to the same problem. In C++, it is faster to use (or std::ranges::sort from
C++20 and onwards). Compared to , the templated is more type-safe since it does not require access to data items through unsafe pointers, as does. Also, accesses the comparison function using a function pointer, necessitating large numbers of repeated function calls, whereas in , comparison functions may be
inlined into the custom object code generated for a template instantiation. In practice, C++ code using is often considerably faster at sorting simple data like integers than equivalent C code using . == References ==