Re: Mergesort algorithm for linked lists
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Sun, 28 Jan 2007 11:51:12 -0500
pete wrote:
Googmeister wrote:
pete <pfil...@xxxxxxxxxxxxxx> wrote:
Stephen Howe wrote:
The worst case for natural mergesort is if the data is in
reverse order to start with. There are no "runs" then.
That's completely wrong for stable mergesorting of linked lists
and mostly wrong for stable mergesorting of arrays.
Mergesorting a reverse ordered list requires the minimum
number of comparisons for a given number of elements;
that is the same number of comparisons required for stable
mergesorting of an already sorted list of same size.
He's talking about *natural* mergesort. Natural mergesorting
a sorted linked list of n elements takes n-1 comparisons.
I am unfamiliar with "*natural* mergesort".
There has been a long discussion on comp.lang.c. This thread is a
late offshoot.
I don't think a reverse sorted input is the worst case for
natural mergesort, but it's proportional to n log n comparisons.
(The reason reverse-sorted is not worst-case is that when you're
merging two lists, you will stop performing comparisons as soon
as one list is exhausted.)
If the occurance of pre-sorted lists is likely, you can do a
preliminary scan to decide if the list is sorted, reverse-sorted,
or unsorted. If sorted, nothing to do. If reverse sorted list
reversal is O(n). Otherwise you can accept the O(NlogN) of
mergesort. At most the preliminary test has added to the constant
factor. Your data will govern whether or not it is worthwhile.
Note that you can abandon the preliminary scan early once lack of
any sorting is detected.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
.
- Follow-Ups:
- Re: Mergesort algorithm for linked lists
- From: Joerg Schoen
- Re: Mergesort algorithm for linked lists
- References:
- Re: Mergesort algorithm for linked lists
- From: Stephen Howe
- Re: Mergesort algorithm for linked lists
- From: pete
- Re: Mergesort algorithm for linked lists
- From: Googmeister
- Re: Mergesort algorithm for linked lists
- From: pete
- Re: Mergesort algorithm for linked lists
- Prev by Date: Re: just starting
- Next by Date: Re: Mergesort algorithm for linked lists
- Previous by thread: Re: Mergesort algorithm for linked lists
- Next by thread: Re: Mergesort algorithm for linked lists
- Index(es):
Relevant Pages
|