Wanted: tricky algorithm

Robert C. White Jr. rwhite at nusdhub.UUCP
Tue Apr 18 05:29:05 AEST 1989


in article <749 at acorn.co.uk>, enevill at acorn.co.uk (Edward Nevill) says:
> Does anyone have, or can anyone think of an algorithm to
> do the above without using an auxiliary array and with a
> fairly decent running order (ie < N*N).

The clasic "heap sort" is an in-place sort useful for such things,
though it wouoldn't be that useful here becuse you have an index into the
memory in the form of your array of pointers.  If you are going to
do anything in-place with the block of memory itself your strings
would need to be maximally aligned (e.g. alligned w.r.t. the longest
string.) and you would need one temporary holder string; then preform
a modified insertion-exchange: remove the string in lowest memory to
the temporary variable and adjust it's pointer; move the losest value
into the lowest (not vacant) slot; place the temporary back into the
space vacated by the exchange; advance the low water mark; repeat.
"back-pointers" from memory are helpful, but not necessary.

If you can approprate a large enough block of memory a sequential
copy using the indexing array will yeild a Big-O of N.  If you
don't have the memory but you do have the time, you could build a
memory image file (laid out as you would lay out the memory block
exactly) and then bulk-load the file back into place in memory.
This will also produce a big-O of N, but the elementary size of each
cycle is somewhat larger/slower because of the file opp.

You have, however piqued my curisoty, what do you need with the sorted
memory block while you have the pointer array.  I's not that I can't
think of a few reasons myself, but most of the ones that come to mind
first are rahter unlikely.

Rob.

Disclamer:  These opinions belong to no one; they are however 
copyrighted by a small corporation in the caman islands.  The
D.E.A. is investigating...



More information about the Comp.lang.c mailing list