|
Lines 211-216
Link Here
|
| 211 |
did not permit the comparison routine itself to call |
211 |
did not permit the comparison routine itself to call |
| 212 |
.Fn qsort 3 . |
212 |
.Fn qsort 3 . |
| 213 |
This is no longer true. |
213 |
This is no longer true. |
|
|
214 |
.Sh EXAMPLES |
| 215 |
Sort an array of integers. |
| 216 |
.Bd -literal |
| 217 |
#include <stdio.h> |
| 218 |
#include <stdlib.h> |
| 219 |
#include <string.h> |
| 220 |
|
| 221 |
/* |
| 222 |
* Custom comparison function |
| 223 |
*/ |
| 224 |
static int |
| 225 |
str_compare(const void *p1, const void *p2) |
| 226 |
{ |
| 227 |
|
| 228 |
/* Cast and dereference */ |
| 229 |
if (*(int *)p1 < *(int *)p2) |
| 230 |
return (-1); |
| 231 |
|
| 232 |
if (*(int *)p1 > *(int *)p2) |
| 233 |
return (1); |
| 234 |
|
| 235 |
return (0); |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
/* |
| 240 |
* Sort an array of integers |
| 241 |
*/ |
| 242 |
int |
| 243 |
main(int argc, char **argv) |
| 244 |
{ |
| 245 |
int i; |
| 246 |
int int_array[] = {4, 5, 9, 3, 1, 7, 2, 8, 6}; |
| 247 |
const int array_size = sizeof(int_array) / sizeof(int); |
| 248 |
|
| 249 |
/* Sort array */ |
| 250 |
qsort(&int_array, array_size, sizeof(int), str_compare); |
| 251 |
|
| 252 |
/* Print out sorted array */ |
| 253 |
for (i = 0; i < array_size; i++) { |
| 254 |
printf("%d\n", int_array[i]); |
| 255 |
} |
| 256 |
|
| 257 |
exit(EXIT_SUCCESS); |
| 258 |
} |
| 259 |
.Ed |
| 214 |
.Sh ERRORS |
260 |
.Sh ERRORS |
| 215 |
The |
261 |
The |
| 216 |
.Fn heapsort |
262 |
.Fn heapsort |