Sorting linked list

VLD/VMB gwyn at BRL.ARPA
Sat Mar 29 15:17:54 AEST 1986


Your method is cute, but it has the following drawbacks:
	It amounts to an insertion sort, with high overhead
	due to the large number of function calls, 2N-1 for
	an N-long list.
	The average number of comparisons for an N-long
	randomly-ordered list is on the order of N(N-1)/4,
	i.e. O(N^2), which is not the best attainable for
	large N.  (At least the function call overhead
	dwindles to insignificance by comparison.)
	Despite your claim, it does require extra space
	(on the run-time stack) proportional to the list size.
This algorithm might be suitable for relatively short lists
where code size is an important factor (e.g. ROM application).

Using recursion like this is good for quick hacks, but for
serious production work nothing beats a good algorithm.
The standard reference for sorting algorithms is Knuth Vol. 3.

P.S.  I did the above math in my head, away from reference
books.  I may be off a factor of two or something similar;
that doesn't affect the argument.



More information about the Comp.lang.c mailing list