qsort.3 (8ce3e01e09403c2f34b7fd373a301e0f2eaa1504) qsort.3 (302318d549dbac9e4803c93259cc1e590776774c)
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

--- 206 unchanged lines hidden (view full) ---

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>
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

--- 206 unchanged lines hidden (view full) ---

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{
223
224/*
225 * Custom comparison function that can compare 'int' values through pointers
226 * passed by qsort(3).
227 */
228static int
229int_compare(const void *p1, const void *p2)
230{
232 int *left = (int *)p1;
233 int *right = (int *)p2;
231 int left = *(const int *)p1;
232 int right = *(const int *)p2;
234
233
235 if (*left < *right)
236 return (-1);
237 else if (*left > *right)
238 return (1);
239 else
240 return (0);
234 return ((left > right) - (left < right));
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 };
235}
236
237/*
238 * Sort an array of 'int' values and print it to standard output.
239 */
240int
241main(void)
242{
243 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;
244 const size_t array_size = sizeof(int_array) / sizeof(int_array[0]);
245 size_t k;
252
246
253 qsort(&int_array, array_size, sizeof(int), int_compare);
247 qsort(&int_array, array_size, sizeof(int_array[0]), int_compare);
254 for (k = 0; k < array_size; k++)
255 printf(" %d", int_array[k]);
248 for (k = 0; k < array_size; k++)
249 printf(" %d", int_array[k]);
256 printf("\\n");
257 exit(EXIT_SUCCESS);
250 puts("");
251 return (EXIT_SUCCESS);
258}
259.Ed
260.Sh ERRORS
261The
262.Fn heapsort
263and
264.Fn mergesort
265functions succeed unless:

--- 67 unchanged lines hidden ---
252}
253.Ed
254.Sh ERRORS
255The
256.Fn heapsort
257and
258.Fn mergesort
259functions succeed unless:

--- 67 unchanged lines hidden ---