QSORT
PRODUCT : TURBO C NUMBER : 398
VERSION : 1.0
OS : PC-DOS
DATE : January 6, 1988 PAGE : 1/3
TITLE : QSORT
qsort is an implementation of the Quicksort algorithm and
requires four parameters: the array to sort, the length of the
array, the width of the array, and a comparison function.
qsort 's length and width parameters are fairly straightforward.
If you are sorting an array declared as:
int array[100];
the length of the array is 100 and the width of the array is
"sizeof(int)". Generally, the length and width of any array can
be expressed as: "sizeof(array)/sizeof(array[0])" (the size of
array divided by the size of the first element of array), and
"sizeof(array[0])" (the size of the first element of array)
respectively.
The fourth parameter (the comparison routine) usually causes the
most trouble. The reference guide states that the fourth
parameter is a pointer to a function. This means that you must
give the name of a routine as the fourth parameter; you must
supply this routine yourself.
qsort will call your routine to do all comparisons. Pointers
will be passed to the array elements which are to be compared.
You do not have to be concerned how this is done, qsort will
handle this for you. Your routine must return an integer, the
value of which will be:
positive - if the first element is greater than the second.
0 - if the elements are equal.
negative - if the second element is greater than the first.
To sort the above array in ascending order, the routine is:
int fcmp(int *num1, int *num2)
{
return (*num1 - *num2);
}
qsort can also sort arrays of structures. If the array was
defined:
PRODUCT : TURBO C NUMBER : 398
VERSION : 1.0
OS : PC-DOS
DATE : January 6, 1988 PAGE : 2/3
TITLE : QSORT
struct address
{
char LastName[16];
char FirstName[11];
char Street[30];
char City[21];
char State[3];
char Zip[6];
} AddressBook[100];
To sort "AddressBook" on "LastName" the fcmp routine would be:
int fcmp(struct address *first, struct address *second)
{
return (strcmp(first->LastName,second->LastName));
}
To sort the same array on "Zip" in the above routine, simply
change "LastName" to "Zip". The call to qsort for both examples
would look like:
qsort(AddressBook, sizeof(AddressBook)/sizeof(AddressBook[0]),
sizeof(AddressBook[0]), fcmp);
The best way to put this all together is a sample program.
/* The program will generate 100 random numbers. Store them
* in an array. Print them out. Call qsort to sort them
* and print them out again.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARRAYLEN 100
int array[ARRAYLEN]; /* Array to sort */
PRODUCT : TURBO C NUMBER : 398
VERSION : 1.0
OS : PC-DOS
DATE : January 6, 1988 PAGE : 3/3
TITLE : QSORT
/* Comparison routine passed to and used by qsort() */
int fcmp(int *num1, int *num2)
{
return (*num1 - *num2);
}
main()
{
long along;
int i;
/* Seed the random number generator */
srand((unsigned) time(&along));
for (i=0; i<ARRAYLEN; i++)
array[i] = rand() % ARRAYLEN;
/* Print the random array */
printf("\nOriginal array\n");
for (i=0; i<ARRAYLEN; i++)
{
printf("%4d ",array[i]);
if ( !((i+1) % 15) )
putchar('\n');
}
/* Call qsort() */
qsort(array,sizeof(array)/sizeof(*array),sizeof(*array),fcmp);
/* Print the sorted array */
printf("\n\nSorted array\n");
for (i=0; i<ARRAYLEN; i++)
{
printf("%4d ",array[i]);
if ( !((i+1) % 15) )
putchar('\n');
}
}
DISCLAIMER: You have the right to use this technical information
subject to the terms of the No-Nonsense License Statement that
you received with the Borland product to which this information
pertains.
Comments
Post a Comment