Output of file : TI398.ASC contained in archive : BCPPTECH.ZIP

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

{
char LastName[16];
char FirstName[11];
char Street[30];
char City[21];
char State[3];
char Zip[6];

To sort "AddressBook" on "LastName" the fcmp routine would be:

{
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:

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
#include
#include

#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 array[i] = rand() % ARRAYLEN;

/* Print the random array */
printf("\nOriginal array\n");
for (i=0; 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 {
printf("%4d ",array[i]);
if ( !((i+1) % 15) )
putchar('\n');
}
}

### 3 Responses to “Category : C Source CodeArchive   : BCPPTECH.ZIPFilename : TI398.ASC”

1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/