From: R.E.Wolff@BitWizard.nl (Rogier Wolff)
To: Helge Hafting <helgehaf@idb.hist.no>
Cc: Manoj Sontakke <manojs@sasken.com>, linux-kernel@vger.kernel.org
Subject: Re: quicksort for linked list
Date: Fri, 9 Mar 2001 12:52:22 +0100 (MET) [thread overview]
Message-ID: <200103091152.MAA31645@cave.bitwizard.nl> (raw)
In-Reply-To: <3AA89624.46DBADD7@idb.hist.no> from Helge Hafting at "Mar 9, 2001 09:36:52 am"
Helge Hafting wrote:
> Manoj Sontakke wrote:
> >
> > Hi
> > Sorry, these questions do not belog here but i could not find any
> > better place.
> >
> > 1. Is quicksort on doubly linked list is implemented anywhere? I need it
> > for sk_buff queues.
>
> I cannot see how the quicksort algorithm could work on a doubly
> linked list, as it relies on being able to look
> up elements directly as in an array.
It took me a few moments to realize, but quicksort is one algorithm
that DOES NOT rely on directly accessing array elements.
qsort (items)
{
if (numberof (items) <= 1) return.
pivot = chose_pivot (items)
for (all items)
if (curitem < pivot) put on the left of pivot
else put on the right of pivot
qsort (items on the left of pivot);
qsort (items on the right of pivot);
}
All operations are easily done on lists, not only on arrays. Actually,
the array-implementation has a few thingies to avoid having to move
the half the array on the scan of one item. With a list that is not an
issue.
If you know how you chose your pivot, one of the "puts" can be
nil. (for example, chose the pivot as the leftmost item. All other
items are already on the right. So "put on the left of pivot" is
"unlink (curitem), relink_to_the_left (pivot, curitem)", but put on
the right is "/* nothing to be done */".
Quicksort however is an algorithm that is recursive. This means that
it can use unbounded amounts of stack -> This is not for the kernel.
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"?
Quicksort has a very bad "worst case": quadratic sort-time. Are you
sure this won't happen?
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.
Roger.
--
** R.E.Wolff@BitWizard.nl ** http://www.BitWizard.nl/ ** +31-15-2137555 **
*-- BitWizard writes Linux device drivers for any device you may have! --*
* There are old pilots, and there are bold pilots.
* There are also old, bald pilots.
next prev parent reply other threads:[~2001-03-09 11:53 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-03-09 7:38 quicksort for linked list Manoj Sontakke
2001-03-09 8:36 ` Helge Hafting
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 11:52 ` Rogier Wolff [this message]
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
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 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=200103091152.MAA31645@cave.bitwizard.nl \
--to=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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.