Re: Shuffling a linked list
- From: "Googmeister" <googmeister@xxxxxxxxx>
- Date: 29 Dec 2006 10:52:43 -0800
Stephen Howe wrote:
There may well be a more elegant O(n*log n) algorithm but doesn't occur
to me offhand.
What about using quick sort for linked lists but suppy a compare which gives
a random result on comparison?
Unless it breaks the quick sort, net result will be it will be randomly
shuffled.
And since quick sort for linked lists is O(n * log n), so will the shuffle
There is an O(n log n) expected time algorithm based on quicksort,
but you need to be careful about how you implement the
random compare (since an element with participate in more than
one comparison and the comparisons must be consistent).
A reasonable way to implement the random compare is to do it
bit-by-bit. Flip a fair coin for each element and put it on the left
list or the right list accordingly, i.e., use the first bit for the
partitioning element. Then recur on each side.
An algorithm based on mergesort can get you the O(n log n) guaranteed.
.
- References:
- Shuffling a linked list
- From: A. Farber
- Re: Shuffling a linked list
- From: Pascal Bourguignon
- Re: Shuffling a linked list
- From: Richard Harter
- Re: Shuffling a linked list
- From: Stephen Howe
- Shuffling a linked list
- Prev by Date: Re: Shuffling a linked list
- Next by Date: Worst case execution time problem
- Previous by thread: Re: Shuffling a linked list
- Next by thread: Re: Shuffling a linked list
- Index(es):
Relevant Pages
|