It is already written.
It is already debugged.
It has been optimized as much as possible (usually).
The algorithm used by qsort() is generally the quick sort algorithm, developed by C. A. R. Hoare in 1962.
Here is the prototype for qsort():
void qsort(void *buf, size_t num, size_t size,
int (*comp)(const void *ele1, const void *ele2));
The qsort() function takes a pointer to an array of user-defined data (buf). The array has num elements in it, and each element is size bytes long. Decisions about sort order are made by calling comp, which is a pointer to a function that compares two elements of buf and returns a value that is less than, equal to, or greater than 0 according to whether the ele1 is less than, equal to, or greater than ele2.
For instance, say you want to sort an array of strings in alphabetical order. The array is terminated by a NULL pointer. Below example shows the function sortStrings(), which sorts a NULL-terminated array of character strings using the qsort() function.
An example of using qsort().
The for loop on lines 19 and 20 simply counts the number of elements in the array so that the count can be passed to qsort(). The only “tricky” part about this code is the comp() function. Its sole purpose is to bridge the gap between the types that qsort() passes to it (const void *) and the types expected by strcmp() (const char *). Because qsort() works with pointers to elements, and the elements are themselves pointers, the correct type to cast ele1 and ele2 to is const char **. The result of the cast is then dereferenced (by putting the * in front of it) to get the const char * type that strcmp() expects.
Given that qsort() exists, why would a C programmer ever write another sort program? There are several reasons. First, there are pathological cases in which qsort() performs very slowly and other algorithms perform better. Second, some overhead is associated with qsort() because it is general purpose. For instance, each comparison involves an indirect function call through the function pointer provided by the user. Also, because the size of an element is a runtime parameter, the code to move elements in the array isn’t optimized for a single size of element. If these performance considerations are important, writing a sort routine might be worth it.
Besides the drawbacks mentioned, the qsort() implementation assumes that all the data is in one array. This might be inconvenient or impossible given the size or nature of the data. Lastly, qsort() implementations are usually not “stable” sorts.
1 comment: