xref: /illumos-gate/usr/src/man/man3c/qsort.3c (revision 014740de)
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 October 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.Ft void
39.Fo qsort_r
40.Fa "void *base"
41.Fa "size_t nel"
42.Fa "size_t width"
43.Fa "int (*compar_arg)(const void *, const void *, void *)"
44.Fa "void *arg"
45.Fc
46.Sh DESCRIPTION
47The
48.Fn qsort
49function is an implementation of the quick-sort algorithm.
50It sorts a table of data in place.
51The contents of the table are sorted in ascending order according to the
52user-supplied comparison function.
53.Pp
54The
55.Fa Ibase
56argument points to the element at the base of the table.
57The
58.Fa nel
59argument is the number of elements in the table.
60The
61.Fa width
62argument specifies the size of each element in bytes.
63The
64.Fa compar
65arguments that point to the elements being compared.
66The comparison function need not compare every byte, so arbitrary data may be
67contained in the elements in addition to the values being compared.
68.Pp
69The function must return an integer less than, equal to, or greater than zero
70to indicate if the first argument is to be considered less than, equal to, or
71greater than the second argument.
72.Pp
73The contents of the table are sorted in ascending order according to the user
74supplied comparison function.
75The relative order in the output of two items that compare as equal is
76unpredictable.
77.Pp
78The
79.Fn qsort_r
80function behaves similarly to the
81.Fn qsort
82function, except that its comparison function,
83.Fn compar_arg ,
84takes an extra argument which is the
85.Fn qsort_r
86argument
87.Fa arg .
88This allows one to avoid global data in the comparison function, unlike
89with the
90.Fn qsort
91function.
92.Pp
93The
94.Fn qsort
95and
96.Fn qsort_r
97function safely allows concurrent access by multiple threads
98to disjoint data, such as overlapping subtrees or tables.
99.Sh EXAMPLES
100.Sy Example 1
101Program sorts.
102.Pp
103The following program sorts a simple array:
104.Bd -literal
105#include <stdlib.h>
106#include <stdio.h>
107
108static int
109intcompare(const void *p1, const void *p2)
110{
111    int i = *((int *)p1);
112    int j = *((int *)p2);
113
114    if (i > j)
115        return (1);
116    if (i < j)
117        return (-1);
118    return (0);
119}
120
121int
122main()
123{
124    int i;
125    int a[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
126    size_t nelems = sizeof (a) / sizeof (int);
127
128    qsort((void *)a, nelems, sizeof (int), intcompare);
129
130    for (i = 0; i < nelems; i++) {
131        (void) printf("%d ", a[i]);
132    }
133
134    (void) printf("\en");
135    return (0);
136}
137.Ed
138.Sh INTERFACE STABILITY
139.Sy Standard
140.Sh MT-LEVEL
141.Sy MT-Safe
142.Sh SEE ALSO
143.Xr sort 1 ,
144.Xr bsearch 3C ,
145.Xr lsearch 3C ,
146.Xr string 3C ,
147.Xr attributes 5 ,
148.Xr standards 5
149