Showing posts with label qsort(). Show all posts
Showing posts with label qsort(). Show all posts

Tuesday, 3 January 2012

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,