The C qsort() function sorts an array in place. The library chooses the sorting algorithm. It calls the comparator with element pointers and arranges entries according to negative, zero, and positive results.
This function is declared in <stdlib.h>. Its contract matters as much as its call: validate every required precondition, check any documented sentinel, and keep ownership or host effects clear in the surrounding code. After a valid call, the array is ordered according to the comparator. There is no failure return; invalid sizes, pointers, or inconsistent comparators lead to invalid or undefined behavior. The examples below move from the direct call to an edge condition and a practical use.
qsort() sorts an array in place. The library chooses the sorting algorithm. It calls the comparator with element pointers and arranges entries according to negative, zero, and positive results.
Include <stdlib.h> before calling qsort().
After a valid call, the array is ordered according to the comparator. There is no failure return; invalid sizes, pointers, or inconsistent comparators lead to invalid or undefined behavior.
A comparator must be consistent and avoid overflow. qsort() is not required to be stable, so equal elements may change relative order.
The function is specified by the C standard library. Exact limits, locale behavior, environment behavior, allocation capacity, or host effects can still vary where relevant.