[comp.lang.c] Sorting anything with qsort

jludwig@pro-grouch.cts.com (Jerry Ludwig) (03/24/91)

   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.
                        Your friendly Windows developer,
                            Jerry.
----
ProLine:  jludwig@pro-grouch
Internet: jludwig@pro-grouch.cts.com
UUCP:     crash!pro-grouch!jludwig
ARPA:     crash!pro-grouch!jludwig@nosc.mil

stan@Dixie.Com (Stan Brown) (03/25/91)

jludwig@pro-grouch.cts.com (Jerry Ludwig) writes:


>                        Your friendly Windows developer,
                                       ^^^^^^^
			
			Our sympathies are with you -:)

-- 
Stan Brown	P. c. Design 	404-363-2303	Ataant Ga.
(emory|gatech|uunet) rsiatl!sdba!stan				"vi forever"

gwyn@smoke.brl.mil (Doug Gwyn) (03/25/91)

In article <8164@crash.cts.com> jludwig@pro-grouch.cts.com (Jerry Ludwig) writes:
>The problem with sorting ANYTHING with qsort is that it wants an array.
>I had to sort records in a file.

I recommend adapting the sort utility from Kernighan & Plauger's "Software
Tools" for external file sorting.

cjc@ulysses.att.com (Chris Calabrese) (03/26/91)

jludwig@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@ulysses.att.com
Obligatory Quote:	``pher - gr. vb. to schlep.  phospher - to schlep light.philosopher - to schlep thoughts.''