From: David Wragg <dpw@doc.ic.ac.uk>
To: R.E.Wolff@BitWizard.nl (Rogier Wolff)
Cc: Helge Hafting <helgehaf@idb.hist.no>,
Manoj Sontakke <manojs@sasken.com>,
linux-kernel@vger.kernel.org
Subject: Re: quicksort for linked list
Date: 10 Mar 2001 19:09:46 +0000 [thread overview]
Message-ID: <y7r7l1xl9tx.fsf@sytry.doc.ic.ac.uk> (raw)
In-Reply-To: <200103091152.MAA31645@cave.bitwizard.nl>
R.E.Wolff@BitWizard.nl (Rogier Wolff) writes:
> Quicksort however is an algorithm that is recursive. This means that
> it can use unbounded amounts of stack -> This is not for the kernel.
The implementation of Quicksort for arrays demands a recursive
implementation, but for doubly-linked lists there is a trick that
leads to an iterative implementation. You can implement Quicksort
recursively for singly linked lists, so in a doubly-linked list you
have a spare link in each node while you are doing the sort. You can
hide the stack in those links, so the implementation doesn't need to
be explicitly recursive. At the end of the sort, the "next" links are
correct, so you have to go through and fix up the "prev" links.
> Quicksort however is an algorithm that is good for large numbers of
> elements to be sorted: the overhead of a small set of items to sort is
> very large. Is the "normal" case indeed "large sets"?
Good implementations of Quicksort actually give up on Quicksort when
the list is short, and use an algorithm that is faster for that case
(measurements are required to find out where the boundary between a
short list and a long list lies). If the full list to be sorted is
short, Quicksort will never be involved. If that happens to be the
common case, then fine.
> Quicksort has a very bad "worst case": quadratic sort-time. Are you
> sure this won't happen?
Introsort avoids this by modifying quicksort to resort to a mergesort
when the recursion gets too deep.
For modern machines, I'm not sure that quicksort on a linked list is
typically much cheaper than mergesort on a linked list. The majority
of the potential cost is likely to be in the pointer chasing involved
in bringing the lists into cache, and that will be the same for both.
Once the list is in cache, how much pointer fiddling you do isn't so
important. For lists that don't fit into cache, the advantages of
mergesort should become even greater if the literature on tape and
disk sorts applies (though multiway merges rather than simple binary
merges would be needed to minimize the impact of memory latency).
Given this, mergesort might be generally preferable to quicksort for
linked lists. But I haven't investigated this idea thoroughly. (The
trick described above for avoiding an explicit stack also works for
mergesort.)
> Isn't it easier to do "insertion sort": Keep the lists sorted, and
> insert the item at the right place when you get the new item.
Easier? Yes. Slower? Yes. Does its being slow matter? Depends on
the context.
David Wragg
next prev parent reply other threads:[~2001-03-10 19:11 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-03-09 7:38 Manoj Sontakke
2001-03-09 8:36 ` Helge Hafting
2001-03-09 11:52 ` Rogier Wolff
2001-03-09 12:09 ` Thomas Pornin
2001-03-09 18:52 ` Oliver Xymoron
2001-03-10 16:15 ` Jerome Vouillon
2001-03-09 22:29 ` Michal Jaegermann
2001-03-10 18:50 ` Martin Mares
2001-03-10 23:54 ` Michal Jaegermann
2001-03-10 19:09 ` David Wragg [this message]
2001-03-12 19:20 ` Jamie Lokier
2001-03-13 6:59 ` James R Bruce
2001-03-09 18:23 ` Oliver Xymoron
2001-03-09 11:09 ` James R Bruce
2001-03-09 11:50 ` Alan Cox
2001-03-09 18:39 ` Oliver Xymoron
2001-03-09 13:46 ` James Lewis Nance
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=y7r7l1xl9tx.fsf@sytry.doc.ic.ac.uk \
--to=dpw@doc.ic.ac.uk \
--cc=R.E.Wolff@BitWizard.nl \
--cc=helgehaf@idb.hist.no \
--cc=linux-kernel@vger.kernel.org \
--cc=manojs@sasken.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®