1qsort(3C)                Standard C Library Functions                qsort(3C)
2
3
4

NAME

6       qsort - quick sort
7

SYNOPSIS

9       #include <stdlib.h>
10
11       void qsort(void *base, size_t nel, size_t width,
12            int (*compar)(const void *, const void *));
13
14

DESCRIPTION

16       The  qsort() function is an implementation of the quick-sort algorithm.
17       It sorts a table of data in place. The contents of the table are sorted
18       in ascending order according to the  user-supplied comparison function.
19
20
21       The  base argument points to the element at the base of the table.  The
22       nel argument is the number of elements in the table.  The  width  argu‐
23       ment  specifies the size of each element in bytes.  The compar argument
24       is the name of the comparison function, which is called with two  argu‐
25       ments that point to the elements being compared.
26
27
28       The  function  must  return  an integer less than, equal to, or greater
29       than zero to indicate if the first argument is to  be  considered  less
30       than, equal to, or greater than the second argument.
31
32
33       The  contents  of  the table are sorted in ascending order according to
34       the user supplied comparison function.
35

USAGE

37       The qsort() function  safely  allows  concurrent  access  by   multiple
38       threads to disjoint data, such as overlapping subtrees or tables.
39

EXAMPLES

41       Example 1 Program sorts.
42
43
44       The following program sorts a simple array:
45
46
47         #include <stdlib.h>
48         #include <stdio.h>
49
50         static int
51         intcompare(const void *p1, const void *p2)
52         {
53             int i = *((int *)p1);
54             int j = *((int *)p2);
55
56             if (i > j)
57                 return (1);
58             if (i < j)
59                 return (-1);
60             return (0);
61         }
62
63         int
64         main()
65         {
66             int i;
67             int a[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
68             size_t nelems = sizeof (a) / sizeof (int);
69
70             qsort((void *)a, nelems, sizeof (int), intcompare);
71
72             for (i = 0; i < nelems; i++) {
73                 (void) printf("%d ", a[i]);
74             }
75
76             (void) printf("\n");
77             return (0);
78         }
79
80

ATTRIBUTES

82       See attributes(5) for descriptions of the following attributes:
83
84
85
86
87       ┌─────────────────────────────┬─────────────────────────────┐
88       │      ATTRIBUTE TYPE         │      ATTRIBUTE VALUE        │
89       ├─────────────────────────────┼─────────────────────────────┤
90       │Interface Stability          │Standard                     │
91       ├─────────────────────────────┼─────────────────────────────┤
92       │MT-Level                     │MT-Safe                      │
93       └─────────────────────────────┴─────────────────────────────┘
94

SEE ALSO

96       sort(1),  bsearch(3C),  lsearch(3C),  string(3C),  attributes(5), stan‐
97       dards(5)
98

NOTES

100       The comparison function need not compare every byte, so arbitrary  data
101       may  be  contained in the elements in addition to the values being com‐
102       pared.
103
104
105       The relative order in the output of two items that compare as equal  is
106       unpredictable.
107
108
109
110SunOS 5.11                        6 Dec 2004                         qsort(3C)
Impressum