Re: Mergesort algorithm for linked lists
- From: "Stephen Howe" <sjhoweATdialDOTpipexDOTcom>
- Date: Fri, 26 Jan 2007 12:47:10 -0000
Eventually, I came up with my own implementation (see below) which seems
to perform quite well. The algorithm is my own idea - at least I don't know
if anybody else has come up with this before.
I am not sure why the OP had followup's set to comp.lang.c (where it seems
off-topic to me) and not comp.programming (on-topic). I did not notice the
followups.
This is a re-post to comp.programming (SJH)
====
It is known. This is natural mergesort. Robert Sedgewick talked about it in
one of his books.
You can't do a natural mergesort for arrays but it is dead simple for linked
lists.
You exploit any pre-existing order anywhere. I would be exceptionally
exceptionally surprised if it is not discussed in Kuth's 3rd volume
somewhere.
Best of all, the sort will be O(N) if it is already sorted and it does well
on data that has partial orders.
The worst case for natural mergesort is if the data is in reverse order to
start with. There are no "runs" then.
I would be tempted to do a hybrid where all runs were at least 4 elements no
matter what.
This would be to guard against the reverse order distribution to start with.
Stephen Howe
.
- Follow-Ups:
- Re: Mergesort algorithm for linked lists
- From: pete
- Re: Mergesort algorithm for linked lists
- From: Joerg Schoen
- Re: Mergesort algorithm for linked lists
- Prev by Date: Re: Recursive prime checking
- Next by Date: Re: Recursive prime checking
- Previous by thread: Re: Mergesort algorithm for linked lists
- Next by thread: Re: Mergesort algorithm for linked lists
- Index(es):
Relevant Pages
|