Sorting anything with qsort

Chris Calabrese cjc at ulysses.att.com
Tue Mar 26 02:32:36 AEST 1991


jludwig at pro-grouch.cts.com (Jerry Ludwig) writes:
>
>   Recently, (friday, that is) I finished my first program that sort using
>qsort.  The problem with sorting ANYTHING with qsort is that it wants an
>array.  I had to sort records in a file.  Another problem is that qsort
>takes only an unsigned int as the number of arguements and the files I had
>to sort could easily be multiple megs (a record was only 14 bytes).     Now
>all of this was solved easily using multiple qsorts on records read into
>a huge array and then merging the qsorts into the final file. But qsort is
>not an end all for sorting ANYTHING by any stretch of the imagination. 
>qsort is a big help on almost all fixed size elements.

Yes, qsort() is a sort algorithm, not a way of life.

If you want to sort large ammounts of data, the method mentioned above
is much better - sort chunks and merge them.

Aside from the obvious fact that qsort() can only handle a relatively
small number of objects to be sorted, you'll get much better
performance out of the merge stratagy when you have more than about
2 mega bytes of data (a number arrived at by closely guarded secret
methods :-) using lots of data generated locally).  This number is
independant of physical memory size (assuming you have sizably more
than 2mb), but dependands on relative disk/memory/cpu speeds.

Using this method, you can sort very large data sets in a predictable
and reasonable time.

Name:			Christopher J. Calabrese
Brain loaned to:	AT&T Bell Laboratories, Murray Hill, NJ
att!ulysses!cjc		cjc at ulysses.att.com
Obligatory Quote:	``pher - gr. vb. to schlep.  phospher - to schlep light.philosopher - to schlep thoughts.''



More information about the Comp.lang.c mailing list