qsort.3 (165903) | qsort.3 (247014) |
---|---|
1.\" Copyright (c) 1990, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" the American National Standards Committee X3, on Information 6.\" Processing Systems. 7.\" 8.\" Redistribution and use in source and binary forms, with or without --- 16 unchanged lines hidden (view full) --- 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" @(#)qsort.3 8.1 (Berkeley) 6/4/93 | 1.\" Copyright (c) 1990, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" the American National Standards Committee X3, on Information 6.\" Processing Systems. 7.\" 8.\" Redistribution and use in source and binary forms, with or without --- 16 unchanged lines hidden (view full) --- 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" @(#)qsort.3 8.1 (Berkeley) 6/4/93 |
33.\" $FreeBSD: head/lib/libc/stdlib/qsort.3 165903 2007-01-09 00:28:16Z imp $ | 33.\" $FreeBSD: head/lib/libc/stdlib/qsort.3 247014 2013-02-19 23:57:39Z keramida $ |
34.\" | 34.\" |
35.Dd September 30, 2003 | 35.Dd February 20, 2013 |
36.Dt QSORT 3 37.Os 38.Sh NAME 39.Nm qsort , qsort_r , heapsort , mergesort 40.Nd sort functions 41.Sh LIBRARY 42.Lb libc 43.Sh SYNOPSIS --- 162 unchanged lines hidden (view full) --- 206.Pp 207.Rv -std heapsort mergesort 208.Sh COMPATIBILITY 209Previous versions of 210.Fn qsort 211did not permit the comparison routine itself to call 212.Fn qsort 3 . 213This is no longer true. | 36.Dt QSORT 3 37.Os 38.Sh NAME 39.Nm qsort , qsort_r , heapsort , mergesort 40.Nd sort functions 41.Sh LIBRARY 42.Lb libc 43.Sh SYNOPSIS --- 162 unchanged lines hidden (view full) --- 206.Pp 207.Rv -std heapsort mergesort 208.Sh COMPATIBILITY 209Previous versions of 210.Fn qsort 211did not permit the comparison routine itself to call 212.Fn qsort 3 . 213This is no longer true. |
214.Sh EXAMPLES 215A sample program that sorts an array of 216.Vt int 217values in place using 218.Fn qsort , 219and then prints the sorted array to standard output is: 220.Bd -literal 221#include <stdio.h> 222#include <stdlib.h> 223#include <string.h> 224 225/* 226 * Custom comparison function that can compare 'int' values through pointers 227 * passed by qsort(3). 228 */ 229static int 230int_compare(const void *p1, const void *p2) 231{ 232 int *left = (int *)p1; 233 int *right = (int *)p2; 234 235 if (*left < *right) 236 return (-1); 237 else if (*left > *right) 238 return (1); 239 else 240 return (0); 241} 242 243/* 244 * Sort an array of 'int' values and print it to standard output. 245 */ 246int 247main(void) 248{ 249 int int_array[] = { 4, 5, 9, 3, 0, 1, 7, 2, 8, 6 }; 250 const int array_size = sizeof(int_array) / sizeof(int_array[0]); 251 int k; 252 253 qsort(&int_array, array_size, sizeof(int), int_compare); 254 for (k = 0; k < array_size; k++) 255 printf(" %d", int_array[k]); 256 printf("\\n"); 257 exit(EXIT_SUCCESS); 258} 259.Ed |
|
214.Sh ERRORS 215The 216.Fn heapsort 217and 218.Fn mergesort 219functions succeed unless: 220.Bl -tag -width Er 221.It Bq Er EINVAL --- 65 unchanged lines hidden --- | 260.Sh ERRORS 261The 262.Fn heapsort 263and 264.Fn mergesort 265functions succeed unless: 266.Bl -tag -width Er 267.It Bq Er EINVAL --- 65 unchanged lines hidden --- |