xref: /illumos-gate/usr/src/man/man3c/qsort.3c (revision edd580643f2cf1434e252cd7779e83182ea84945)
1.\"
2.\" The contents of this file are subject to the terms of the
3.\" Common Development and Distribution License (the "License").
4.\" You may not use this file except in compliance with the License.
5.\"
6.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
7.\" or http://www.opensolaris.org/os/licensing.
8.\" See the License for the specific language governing permissions
9.\" and limitations under the License.
10.\"
11.\" When distributing Covered Code, include this CDDL HEADER in each
12.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
13.\" If applicable, add the following below this CDDL HEADER, with the
14.\" fields enclosed by brackets "[]" replaced with your own identifying
15.\" information: Portions Copyright [yyyy] [name of copyright owner]
16.\"
17.\"
18.\" Copyright 1989 AT&T
19.\" Copyright (c) 2002, Sun Microsystems, Inc.  All Rights Reserved
20.\" Copyright 2020 Oxide Computer Company
21.\"
22.Dd September 11, 2020
23.Dt QSORT 3C
24.Os
25.Sh NAME
26.Nm qsort ,
27.Nm qsort_r
28.Nd quick sort
29.Sh SYNOPSIS
30.In stdlib.h
31.Ft void
32.Fo qsort
33.Fa "void *base"
34.Fa "size_t nel"
35.Fa "size_t width"
36.Fa "int (*compar)(const void *, const void *)"
37.Fc
38.Fo qsort
39.Fa "void *base"
40.Fa "size_t nel"
41.Fa "size_t width"
42.Fa "int (*compar_arg)(const void *, const void *, void *)"
43.Fa "void *arg"
44.Fc
45.Sh DESCRIPTION
46The
47.Fn qsort
48function is an implementation of the quick-sort algorithm.
49It sorts a table of data in place.
50The contents of the table are sorted in ascending order according to the
51user-supplied comparison function.
52.Pp
53The
54.Fa Ibase
55argument points to the element at the base of the table.
56The
57.Fa nel
58argument is the number of elements in the table.
59The
60.Fa width
61argument specifies the size of each element in bytes.
62The
63.Fa compar
64arguments that point to the elements being compared.
65The comparison function need not compare every byte, so arbitrary data may be
66contained in the elements in addition to the values being compared.
67.Pp
68The function must return an integer less than, equal to, or greater than zero
69to indicate if the first argument is to be considered less than, equal to, or
70greater than the second argument.
71.Pp
72The contents of the table are sorted in ascending order according to the user
73supplied comparison function.
74The relative order in the output of two items that compare as equal is
75unpredictable.
76.Pp
77The
78.Fn qsort_r
79function behaves similarly to the
80.Fn qsort
81function, except that its comparison function,
82.Fn compar_arg ,
83takes an extra argument which is the
84.Fn qsort_r
85argument
86.Fa arg .
87This allows one to avoid global data in the comparison function, unlike
88with the
89.Fn qsort
90function.
91.Pp
92The
93.Fn qsort
94and
95.Fn qsort_r
96function safely allows concurrent access by multiple threads
97to disjoint data, such as overlapping subtrees or tables.
98.Sh EXAMPLES
99.Sy Example 1
100Program sorts.
101.Pp
102The following program sorts a simple array:
103.Bd -literal
104#include <stdlib.h>
105#include <stdio.h>
106
107static int
108intcompare(const void *p1, const void *p2)
109{
110    int i = *((int *)p1);
111    int j = *((int *)p2);
112
113    if (i > j)
114        return (1);
115    if (i < j)
116        return (-1);
117    return (0);
118}
119
120int
121main()
122{
123    int i;
124    int a[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
125    size_t nelems = sizeof (a) / sizeof (int);
126
127    qsort((void *)a, nelems, sizeof (int), intcompare);
128
129    for (i = 0; i < nelems; i++) {
130        (void) printf("%d ", a[i]);
131    }
132
133    (void) printf("\en");
134    return (0);
135}
136.Ed
137.Sh INTERFACE STABILITY
138.Sy Standard
139.Sh MT-LEVEL
140.Sy MT-Safe
141.Sh SEE ALSO
142.Xr sort 1 ,
143.Xr bsearch 3C ,
144.Xr lsearch 3C ,
145.Xr string 3C ,
146.Xr attributes 5 ,
147.Xr standards 5
148