function definition?!

Hello.
Please tell me how the 'compare function is used in the example at:
http://www.cplusplus.com/reference/cstdlib/qsort/

Thanks in advance
The idea is this, the sort function is independent of the algorithm. All the sort process needs to know about your data is the number of elements, the size of each element, how to copy and how to compare elements.

Assignment in C is done using a bitwize copy. qsort requires all the others as parameters:
1. the array base
2. the array size
3. the number of elements in the array
4. the compare function
http://pubs.opengroup.org/onlinepubs/007908799/xsh/qsort.html

The compare function returns -1 for less, 0 for equals, 1 for greater than. qsort calls it when it needs to compare elements and that is the basis of whether elements are swapped during the sort process.

I feel I haven't said anything that's not already there, but there's not much else to say.
Last edited on
Topic archived. No new replies allowed.