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 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. 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
#include <stdlib.h>
#include <conio.h>
/*
author :-Sailesh Kumar
Company :-saileshkumar.org
From :-YongsterS
* This routine is used only by sortStrings(), to provide a
* string comparison function to pass to qsort().
*/
static int comp(const void *ele1, const void *ele2)
{
return strcmp(*(const char **) ele1,
*(const char **) ele2);
}
/* Sort strings using the library function qsort() */
void sortStrings(const char *array[])
{
/* First, determine the length of the array */
int num;
for (num = 0; array[num]; num++)
qsort(array, num, sizeof(*array), comp);
}
Use this to Execute ur program in VTU Externals!!