Sorting linked list

Wayne A. Christopher faustus at cad.UUCP
Fri Mar 28 10:09:20 AEST 1986


In article <669 at bentley.UUCP>, mdr at bentley.UUCP (M. Rossner) writes:
> It seems that there is an obvious recursive solution to this
> problem that has not yet been mentioned.  To sort a linked list
> (in place, without allocating ANY more memory), I just need to sort
> everything except the first node and then insert the first node into
> this list.

If you're concerned with the amount of space you use, you should be taking
the stack space that your process is using up by all that recursion.  Also
insertion sort isn't the fastest that you can do -- I think making an array,
qsort()'ing it, and re-listing it is the best you can do.  Besides, nowadays
it's not fashonable to be concerned with saving space -- it's all virtual
anyway... :-)

> Voila! Less than tweny lines of code in its entirety.  Think recursive, man!

In C you shouldn't automatically think in terms of recursion, since it is
a lot easier to write fast iterative code.  Recursion is more elegant but
seldom faster.

	Wayne



More information about the Comp.lang.c mailing list