分类: C/C++
2006-12-12 18:17:21
Sort using quicksort algorith.
This function uses an implementation of the quicksort algorithm to sort the num elements of an array pointed by base, each element has the specified width in bytes.
The method used to compare each pair of elements is provided by the caller to this function with fncompare parameter, that is a function called one or more times during the sort process.
Parameters.
int fncompare (const void * elem1, const void * elem2 );The function should receive two parameters (elem1 and elem2) that are pointers to elements, and should return an int value with the result of comparing them:
return value description <0 *elem1 goes before *elem2 0 *elem1 == *elem2 >0 *elem1 goes after *elem2
Return Value.
(none)
Portability.
Defined in ANSI-C.
Example./* qsort example */
Output:
#include
#include
int values[] = { 40, 10, 100, 90, 20, 25 };
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main ()
{
int * pItem;
int n;
qsort (values, 6, sizeof(int), compare);
for (n=0; n<6; n++)
{
printf ("%d ",values[n]);
}
return 0;
}
10 20 25 40 90 100