What is the easiest sorting method to use
The answer is the standard library function qsort(). It’s the easiest sort by far for several reasons:
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 pointerto 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. Listing III.1 shows the function sortStrings(), which sorts a NULL-terminated array of character
strings using the qsort() function. You can compile this example into a working program using the code
found at the end of this chapter.
#include <stdlib.h>
2:
3: /*
4: * This routine is used only by sortStrings(), to provide a
5: * string comparison function to pass to qsort().
6: */
7: static int comp(const void *ele1, const void *ele2)
8: {
9: return strcmp(*(const char **) ele1,
10: *(const char **) ele2);
11: }
12:
13: /* Sort strings using the library function qsort() */
14: void sortStrings(const char *array[])
15: {
16 : /* First, determine the length of the array */
17: int num;
18:
19: for (num = 0; array[num]; num++)
20: ;
21: qsort(array, num, sizeof(*array), comp);
22: }
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 algorithmsperform better. Second, some overhead is associated with qsort() because it is general purpose. For instance reach 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.
Cross Reference:
III.2: What is the quickest sorting method to use?
III.3: How can I sort things that are too large to bring into memory?
III.7: How can I sort a linked list?
VII.1: What is indirection?
VII.2: How many levels of pointers can you have?
VII.5: What is a void pointer?
VII.6: When is a void pointer used?
No comments:
Post a Comment