Concept
What is a comparator function and how does it determine element order?
ComputerScienceOne / Comparator Functions
"A generic Quick Sort algorithm specifies how to sort elements, but it doesn’t specify how they are ordered. Essentially, Quick Sort needs to know when two elements, a and b, are in order, out of order, or equivalent in order to decide which partition each element goes in. However, it doesn’t “know” anything about the elements a and b themselves. They could be numbers, strings, or user-defined objects. A sorting algorithm still needs to be able to determine the proper ordering in order to sort. In C this is achieved through a comparator function, which is a function that is responsible for comparing two elements and determining their proper order.\n\nA comparator function has the following signature and behavior:\n\nint cmp(const void *a, const void *b);\n\nThe function takes two generic void * pointers which refer to the elements being compared. Moreover, the const keyword is used to indicate that no changes will be made to the elements. The function returns an integer indicating the relative ordering of the two elements: it returns something negative, < 0, if a comes before b; it returns zero if a and b are equal; and it returns something positive, > 0, if a comes after b. There is no guarantee on the value’s magnitude, so it does not necessarily return −1 or +1; it just returns something negative or positive.\n\nThis pattern was previously seen when comparing strings. The standard string library provides strcmp(), which has the same basic contract: it takes two strings and returns something negative, zero, or something positive depending on the lexicographic ordering of the two strings. Strictly speaking, however, strcmp() is not a comparator function because it is defined to take two const char * parameters, not const void * pointers. The C language and compiler know how to compare built-in primitive types like int and double using built-in comparison operators. To generalize the comparison operation, void * pointers are used so that general comparator functions can be written and used in generic searching and sorting functions."
Related Ideas
- How does a Comparator determine the order of two elements?ComputerScienceOne · Comparators
- How can comparator functions order Student structures?ComputerScienceOne · Comparator Functions
- How does the C standard library’s qsort() function work?ComputerScienceOne · Sorting
- What is a function pointer and how is it declared?ComputerScienceOne · Function Pointers
- How does strcmp() compare the contents of two strings in C?ComputerScienceOne · Comparisons
- How can qsort() sort an array of strings using pointers?ComputerScienceOne · Sorting Pointers to Elements
- How should a comparator handle an array of structure pointers with NULL values?ComputerScienceOne · Sorting Pointers to Elements
- What are the advantages of using qsort() and related generic functions?ComputerScienceOne · Sorting