Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdlib.h

C qsort() function

Reference for C qsort(): sort any array with a comparison callback, write correct comparators for ints, doubles, strings and structs, and avoid overflow bugs.

qsort() sorts an array of any element type in place. Because C has no generics, you pass the array, the number of elements, the size of one element, and a comparison function that tells qsort which of two elements comes first. The name suggests quicksort, but the standard does not say which algorithm is used and the sort is not guaranteed to be stable.

C
#include <stdlib.h>

void qsort(void *base, size_t nmemb, size_t size,
           int (*compar)(const void *, const void *));

Try it

C Sorting ints, doubles and strings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Sorting structs by two keys

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
void qsort(void *base, size_t nmemb, size_t size,
           int (*compar)(const void *, const void *));
Name Description
base Pointer to the first element of the array.
nmemb Number of elements.
size Size in bytes of one element (sizeof arr[0]).
compar Receives pointers to two elements. Returns a negative number if the first should come before the second, zero if they are equal, positive if it should come after.

Returns: nothing. The array is sorted in place.

Errors / undefined behavior: an inconsistent comparator (for example one that is not a strict weak ordering) makes the result unspecified and can crash some implementations. Passing a null base with nmemb > 0, or a wrong size, is undefined behavior. The size times nmemb product must not overflow.

Usage notes

  • The comparator receives pointers to elements, not elements. For an int array cast to const int *; for an array of char * the arguments point at char *, so you need *(const char *const *)a. Passing strcmp directly to sort strings is a classic mistake.
  • Never return x - y for ints. The subtraction overflows for extreme values. The idiom (x > y) - (x < y) is safe and branch-free.
  • Doubles and NaN: with NaN every comparison is false, so a naive comparator is inconsistent. Decide where NaNs go and handle them first.
  • Not stable. Equal elements may end up in any order. Add a tiebreaker (as in the second example, or an original-index field) when order of equal items matters.
  • Complexity: typically O(n log n) comparisons; the algorithm differs between C libraries and versions. The comparator is called through a pointer, so very hot sorts of plain numbers are faster with a hand-written type-specific sort.
  • Sort pointers, not big structs, when the struct is large: swapping 8-byte pointers is cheaper than swapping kilobytes.
  • C23 and later do not change qsort; there is no qsort_s in glibc (the optional Annex K version).
  • Searching sorted data: use the same comparator convention with bsearch().