Namespaces
Variants
Views
Actions

Difference between revisions of "c/algorithm/qsort"

From cppreference.com
< c‎ | algorithm
m (Since the link to this page mentions qsort_s, this page now mentions qsort_s.)
(damage control for qsort_s)
Line 1: Line 1:
{{c/title|qsort}}
+
{{c/title|qsort|qsort_s}}
 
{{c/algorithm/navbar}}
 
{{c/algorithm/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
Line 9: Line 9:
 
{{dcl | since=c11 |
 
{{dcl | since=c11 |
 
errno_t qsort_s( void *ptr, rsize_t count, rsize_t size,
 
errno_t qsort_s( void *ptr, rsize_t count, rsize_t size,
                 int (*comp)(const void *, const void *, void *context),
+
                 int (*comp)(const void *, const void *, void *),
 
                 void *context );
 
                 void *context );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
Sorts the given array pointed to by {{tt|ptr}} in ascending order. The array contains {{tt|count}} elements of {{tt|size}} bytes. Function pointed to by {{tt|comp}} is used for object comparison. The {{tt|context}} specifies additional details about the sort.
+
@1@ Sorts the given array pointed to by {{tt|ptr}} in ascending order. The array contains {{tt|count}} elements of {{tt|size}} bytes. Function pointed to by {{tt|comp}} is used for object comparison.  
 +
@2@ Same as {{v|1}}, except that the additional context parameter {{tt|context}} is passed to {{tt|comp}} and that that the following errors are detected at runtime and call the currently installed [[c/error/set_constraint_handler_s|constraint handler]] function:
 +
:* {{tt|count}} or {{tt|size}} is greater than {{lc|RSIZE_MAX}}
 +
:* {{tt|ptr}} or {{tt|compar}} is a null pointer (unless {{tt|count}} is zero)
 +
:As all bounds-checked functions, {{tt|qsort_s}} is only guaranteed to be available if {{c|__STDC_LIB_EXT1__}} is defined by the implementation and if the user defines {{c|__STDC_WANT_LIB_EXT1__}} to the integer constant {{c|1}} before including {{tt|stdlib.h}}.
  
If {{tt|comp}} indicates two elements as equivalent, their order is undefined.
+
If {{tt|comp}} indicates two elements as equivalent, their order in the resulting sorted array is undefined.
 
+
As with all bounds-checked functions, {{tt|qsort_s}} is only guaranteed to be available if {{c|__STDC_LIB_EXT1__}} is defined by the implementation and if the user defines {{c|__STDC_WANT_LIB_EXT1__}} to the integer constant {{c|1}} before including {{tt|<stdlib.h>}}.
+
  
 
===Parameters===
 
===Parameters===
Line 26: Line 28:
 
{{par | size | size of each element in the array in bytes}}
 
{{par | size | size of each element in the array in bytes}}
 
{{par ccmp | comp }}
 
{{par ccmp | comp }}
{{par | context | additional information about the sort (e.g., collating sequence)}}
+
{{par | context | additional information (e.g., collating sequence), passed to {{tt|comp}} as the third argument}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
(none)
+
@1@ (none)
 +
@2@ zero on success, non-zero if a runtime constraints violation was detected
 +
 
 +
===Notes===
 +
Unlike other bounds-checked functions, {{tt|qsort_s}} does not treat arrays of zero size as a runtime constraint violation and instead returns successfully without altering the array (the other function that accepts arrays of zero size is {{lc|bsearch_s}}).
 +
 
 +
Until {{tt|qsort_s}}, users of {{tt|qsort}} often used global variables to pass additional context to the comparison function.
  
 
===Example===
 
===Example===
Line 76: Line 84:
 
{{ref std | section=7.20.5.2 | title=The qsort function | p=319}}
 
{{ref std | section=7.20.5.2 | title=The qsort function | p=319}}
 
{{ref std c89}}
 
{{ref std c89}}
{{ref std | section= | title=}}
+
{{ref std | section=4.10.5.2 | title=The qsort function}}
 
{{ref std end}}
 
{{ref std end}}
  

Revision as of 06:51, 17 February 2015

Defined in header <stdlib.h>
void qsort( void *ptr, size_t count, size_t size,
            int (*comp)(const void *, const void *) );
errno_t qsort_s( void *ptr, rsize_t count, rsize_t size,

                 int (*comp)(const void *, const void *, void *),

                 void *context );
(since C11)
1) Sorts the given array pointed to by ptr in ascending order. The array contains count elements of size bytes. Function pointed to by comp is used for object comparison.
2) Same as (1), except that the additional context parameter context is passed to comp and that that the following errors are detected at runtime and call the currently installed constraint handler function:
  • count or size is greater than RSIZE_MAX
  • ptr or compar is a null pointer (unless count is zero)
As all bounds-checked functions, qsort_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including stdlib.h.

If comp indicates two elements as equivalent, their order in the resulting sorted array is undefined.

Contents

Parameters

ptr - pointer to the array to sort
count - number of element in the array
size - size of each element in the array in bytes
comp - comparison function which returns ​a negative integer value if the first argument is less than the second, a positive integer value if the first argument is greater than the second and zero if the arguments are equivalent.

The signature of the comparison function should be equivalent to the following:

 int cmp(const void *a, const void *b);

The function must not modify the objects passed to it and must return consistent results when called for the same objects, regardless of their positions in the array.

context - additional information (e.g., collating sequence), passed to comp as the third argument

Return value

1) (none)
2) zero on success, non-zero if a runtime constraints violation was detected

Notes

Unlike other bounds-checked functions, qsort_s does not treat arrays of zero size as a runtime constraint violation and instead returns successfully without altering the array (the other function that accepts arrays of zero size is bsearch_s).

Until qsort_s, users of qsort often used global variables to pass additional context to the comparison function.

Example

The following code sorts an array of integers using qsort()

#include <stdio.h>
#include <stdlib.h>
 
int compare_ints(const void* a, const void* b)
{
    int arg1 = *(const int*)a;
    int arg2 = *(const int*)b;
 
    if (arg1 < arg2) return -1;
    if (arg1 > arg2) return 1;
    return 0;
}
 
int main(void)
{
    int i;
    int ints[] = { -2, 99, 0, -743, 2, 3, 4 };
    int size = sizeof ints / sizeof *ints;
 
    qsort(ints, size, sizeof(int), compare_ints);
 
    for (i = 0; i < size; i++) {
        printf("%d ", ints[i]);
    }
 
    printf("\n");
 
    return EXIT_SUCCESS;
}

Output:

-743 -2 0 2 3 4 99

References

  • C11 standard (ISO/IEC 9899:2011):
  • 7.22.5.2 The qsort function (p: 355-356)
  • K.3.6.3.2 The qsort_s function (p: 609)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.20.5.2 The qsort function (p: 319)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 4.10.5.2 The qsort function

See also

searches an array for an element of unspecified type
(function) [edit]