Steve said:
can someone tell me how qsort function in <stdlib.h> is used
(qsort(..........))?
the function has a buffer, two void * parameters and the a pointer to a
compare function. Thanks.
Have you tried reading a reference manual? Here's what the GNU C
library manual says about qsort().
Defining the Comparison Function
================================
In order to use the sorted array library functions, you have to describe
how to compare the elements of the array.
To do this, you supply a comparison function to compare two elements
of the array. The library will call this function, passing as arguments
pointers to two array elements to be compared. Your comparison function
should return a value the way `strcmp' (*note String/Array
Comparison:
does: negative if the first argument is "less" than the
second, zero if they are "equal", and positive if the first argument is
"greater".
Here is an example of a comparison function which works with an
array of numbers of type `double':
int
compare_doubles (const void *a, const void *b)
{
const double *da = (const double *) a;
const double *db = (const double *) b;
return (*da > *db) - (*da < *db);
}
The header file `stdlib.h' defines a name for the data type of
comparison functions. This type is a GNU extension.
int comparison_fn_t (const void *, const void *);
Array Sort Function
===================
To sort an array using an arbitrary comparison function, use the
`qsort' function. The prototype for this function is in `stdlib.h'.
- Function: void qsort (void *ARRAY, size_t COUNT, size_t SIZE,
comparison_fn_t COMPARE)
The QSORT function sorts the array ARRAY. The array contains
COUNT elements, each of which is of size SIZE.
The COMPARE function is used to perform the comparison on the
array elements. This function is called with two pointer
arguments and should return an integer less than, equal to, or
greater than zero corresponding to whether its first argument is
considered less than, equal to, or greater than its second
argument.
*Warning:* If two objects compare as equal, their order after
sorting is unpredictable. That is to say, the sorting is not
stable. This can make a difference when the comparison considers
only part of the elements. Two elements with the same sort key
may differ in other respects.
If you want the effect of a stable sort, you can get this result by
writing the comparison function so that, lacking other reason
distinguish between two elements, it compares them by their
addresses. Note that doing this may make the sorting algorithm
less efficient, so do it only if necessary.
Here is a simple example of sorting an array of doubles in
numerical order, using the comparison function defined above
(*note Comparison Functions:
:
{
double *array;
int size;
...
qsort (array, size, sizeof (double), compare_doubles);
}
The `qsort' function derives its name from the fact that it was
originally implemented using the "quick sort" algorithm.
The implementation of `qsort' in this library might not be an
in-place sort and might thereby use an extra amount of memory to
store the array.