xref: /illumos-gate/usr/src/man/man3c/qsort.3c (revision 8c9d84f540d1f878958d02bef1d5c221fc8d30c0)
144431c82SRobert Mustacchi.\"
244431c82SRobert Mustacchi.\" The contents of this file are subject to the terms of the
344431c82SRobert Mustacchi.\" Common Development and Distribution License (the "License").
444431c82SRobert Mustacchi.\" You may not use this file except in compliance with the License.
544431c82SRobert Mustacchi.\"
644431c82SRobert Mustacchi.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
744431c82SRobert Mustacchi.\" or http://www.opensolaris.org/os/licensing.
844431c82SRobert Mustacchi.\" See the License for the specific language governing permissions
944431c82SRobert Mustacchi.\" and limitations under the License.
1044431c82SRobert Mustacchi.\"
1144431c82SRobert Mustacchi.\" When distributing Covered Code, include this CDDL HEADER in each
1244431c82SRobert Mustacchi.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1344431c82SRobert Mustacchi.\" If applicable, add the following below this CDDL HEADER, with the
1444431c82SRobert Mustacchi.\" fields enclosed by brackets "[]" replaced with your own identifying
1544431c82SRobert Mustacchi.\" information: Portions Copyright [yyyy] [name of copyright owner]
1644431c82SRobert Mustacchi.\"
1744431c82SRobert Mustacchi.\"
1844431c82SRobert Mustacchi.\" Copyright 1989 AT&T
1944431c82SRobert Mustacchi.\" Copyright (c) 2002, Sun Microsystems, Inc.  All Rights Reserved
2044431c82SRobert Mustacchi.\" Copyright 2020 Oxide Computer Company
2144431c82SRobert Mustacchi.\"
22*8c9d84f5SToomas Soome.Dd November 15, 2023
2344431c82SRobert Mustacchi.Dt QSORT 3C
2444431c82SRobert Mustacchi.Os
2544431c82SRobert Mustacchi.Sh NAME
2644431c82SRobert Mustacchi.Nm qsort ,
2744431c82SRobert Mustacchi.Nm qsort_r
2844431c82SRobert Mustacchi.Nd quick sort
2944431c82SRobert Mustacchi.Sh SYNOPSIS
3044431c82SRobert Mustacchi.In stdlib.h
3144431c82SRobert Mustacchi.Ft void
3244431c82SRobert Mustacchi.Fo qsort
3344431c82SRobert Mustacchi.Fa "void *base"
3444431c82SRobert Mustacchi.Fa "size_t nel"
3544431c82SRobert Mustacchi.Fa "size_t width"
3644431c82SRobert Mustacchi.Fa "int (*compar)(const void *, const void *)"
3744431c82SRobert Mustacchi.Fc
3835abe327SRobert Mustacchi.Ft void
3935abe327SRobert Mustacchi.Fo qsort_r
4044431c82SRobert Mustacchi.Fa "void *base"
4144431c82SRobert Mustacchi.Fa "size_t nel"
4244431c82SRobert Mustacchi.Fa "size_t width"
4344431c82SRobert Mustacchi.Fa "int (*compar_arg)(const void *, const void *, void *)"
4444431c82SRobert Mustacchi.Fa "void *arg"
4544431c82SRobert Mustacchi.Fc
4644431c82SRobert Mustacchi.Sh DESCRIPTION
4744431c82SRobert MustacchiThe
4844431c82SRobert Mustacchi.Fn qsort
4944431c82SRobert Mustacchifunction is an implementation of the quick-sort algorithm.
5044431c82SRobert MustacchiIt sorts a table of data in place.
5144431c82SRobert MustacchiThe contents of the table are sorted in ascending order according to the
5244431c82SRobert Mustacchiuser-supplied comparison function.
5344431c82SRobert Mustacchi.Pp
5444431c82SRobert MustacchiThe
55*8c9d84f5SToomas Soome.Fa base
5644431c82SRobert Mustacchiargument points to the element at the base of the table.
5744431c82SRobert MustacchiThe
5844431c82SRobert Mustacchi.Fa nel
5944431c82SRobert Mustacchiargument is the number of elements in the table.
6044431c82SRobert MustacchiThe
6144431c82SRobert Mustacchi.Fa width
6244431c82SRobert Mustacchiargument specifies the size of each element in bytes.
6344431c82SRobert MustacchiThe
6444431c82SRobert Mustacchi.Fa compar
65*8c9d84f5SToomas Soomeargument is the name of the comparison function, which is called with two
66c10c16deSRichard Lowearguments that point to the elements being compared.
6744431c82SRobert MustacchiThe comparison function need not compare every byte, so arbitrary data may be
6844431c82SRobert Mustacchicontained in the elements in addition to the values being compared.
6944431c82SRobert Mustacchi.Pp
70c10c16deSRichard LoweThe function must return an integer less than, equal to, or greater than zero
71c10c16deSRichard Loweto indicate if the first argument is to be considered less than, equal to, or
72c10c16deSRichard Lowegreater than the second argument.
7344431c82SRobert Mustacchi.Pp
74c10c16deSRichard LoweThe contents of the table are sorted in ascending order according to the user
75c10c16deSRichard Lowesupplied comparison function.
7644431c82SRobert MustacchiThe relative order in the output of two items that compare as equal is
7744431c82SRobert Mustacchiunpredictable.
7844431c82SRobert Mustacchi.Pp
7944431c82SRobert MustacchiThe
8044431c82SRobert Mustacchi.Fn qsort_r
8144431c82SRobert Mustacchifunction behaves similarly to the
8244431c82SRobert Mustacchi.Fn qsort
8344431c82SRobert Mustacchifunction, except that its comparison function,
8444431c82SRobert Mustacchi.Fn compar_arg ,
8544431c82SRobert Mustacchitakes an extra argument which is the
8644431c82SRobert Mustacchi.Fn qsort_r
8744431c82SRobert Mustacchiargument
8844431c82SRobert Mustacchi.Fa arg .
8944431c82SRobert MustacchiThis allows one to avoid global data in the comparison function, unlike
9044431c82SRobert Mustacchiwith the
9144431c82SRobert Mustacchi.Fn qsort
9244431c82SRobert Mustacchifunction.
9344431c82SRobert Mustacchi.Pp
9444431c82SRobert MustacchiThe
9544431c82SRobert Mustacchi.Fn qsort
9644431c82SRobert Mustacchiand
9744431c82SRobert Mustacchi.Fn qsort_r
98*8c9d84f5SToomas Soomefunctions safely allow concurrent access by multiple threads
99c10c16deSRichard Loweto disjoint data, such as overlapping subtrees or tables.
10044431c82SRobert Mustacchi.Sh EXAMPLES
10144431c82SRobert Mustacchi.Sy Example 1
10244431c82SRobert MustacchiProgram sorts.
10344431c82SRobert Mustacchi.Pp
104c10c16deSRichard LoweThe following program sorts a simple array:
10544431c82SRobert Mustacchi.Bd -literal
106c10c16deSRichard Lowe#include <stdlib.h>
107c10c16deSRichard Lowe#include <stdio.h>
108c10c16deSRichard Lowe
109c10c16deSRichard Lowestatic int
110c10c16deSRichard Loweintcompare(const void *p1, const void *p2)
111c10c16deSRichard Lowe{
112c10c16deSRichard Lowe    int i = *((int *)p1);
113c10c16deSRichard Lowe    int j = *((int *)p2);
114c10c16deSRichard Lowe
115c10c16deSRichard Lowe    if (i > j)
116c10c16deSRichard Lowe        return (1);
117c10c16deSRichard Lowe    if (i < j)
118c10c16deSRichard Lowe        return (-1);
119c10c16deSRichard Lowe    return (0);
120c10c16deSRichard Lowe}
121c10c16deSRichard Lowe
122c10c16deSRichard Loweint
123c10c16deSRichard Lowemain()
124c10c16deSRichard Lowe{
125c10c16deSRichard Lowe    int i;
126c10c16deSRichard Lowe    int a[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
127c10c16deSRichard Lowe    size_t nelems = sizeof (a) / sizeof (int);
128c10c16deSRichard Lowe
129c10c16deSRichard Lowe    qsort((void *)a, nelems, sizeof (int), intcompare);
130c10c16deSRichard Lowe
131c10c16deSRichard Lowe    for (i = 0; i < nelems; i++) {
132c10c16deSRichard Lowe        (void) printf("%d ", a[i]);
133c10c16deSRichard Lowe    }
134c10c16deSRichard Lowe
135c10c16deSRichard Lowe    (void) printf("\en");
136c10c16deSRichard Lowe    return (0);
137c10c16deSRichard Lowe}
13844431c82SRobert Mustacchi.Ed
13944431c82SRobert Mustacchi.Sh INTERFACE STABILITY
14044431c82SRobert Mustacchi.Sy Standard
14144431c82SRobert Mustacchi.Sh MT-LEVEL
14244431c82SRobert Mustacchi.Sy MT-Safe
14344431c82SRobert Mustacchi.Sh SEE ALSO
14444431c82SRobert Mustacchi.Xr sort 1 ,
14544431c82SRobert Mustacchi.Xr bsearch 3C ,
14644431c82SRobert Mustacchi.Xr lsearch 3C ,
14744431c82SRobert Mustacchi.Xr string 3C ,
148bbf21555SRichard Lowe.Xr attributes 7 ,
149bbf21555SRichard Lowe.Xr standards 7
150