Qsort: Difference between revisions

Content deleted Content added
Reverted 1 edit by Ivoncheung (talk): WP:OR; and, this article is about the standard C function qsort, not about implementing quicksort in C. (TW)
Line 15:
 
/* Comparison function. Receives two generic (void) pointers. */
int compare(const void *p, const void *q) {
{
int ret;
int x = *(const int *)p;
int y = *(const int *)q;
Line 23 ⟶ 21:
/* Avoid return x - y, which can cause undefined behaviour
because of signed integer overflow. */
if (x ==< y)
retreturn = 0-1;
else if (x < y)
ret = -1;
else
ret =return 1;
 
return ret0;
}
 
/* Sort an array of n integers, pointed to by a. */
void sort_ints(int *a, size_t n) {
{
qsort(a, n, sizeof(int), compare);
}