Re: the sort function in lisp (destructive)
- From: Gene <gene.ressler@xxxxxxxxx>
- Date: Fri, 11 Jan 2008 19:46:45 -0800 (PST)
On Jan 7, 1:08 am, Madhu <enom...@xxxxxxxx> wrote:
* Gene <3d734438-8f14-48ab-95f6-46aec08e7...@xxxxxxxxxxxxxxxxxxxxxxxxxxx> :
Wrote on Sun, 6 Jan 2008 13:20:31 -0800 (PST):
|> > In fact looking at the source for Corman Lisp I discovered that it
|> > reads the CAR into a vector, sorts that, and then puts the values
|> > back in the CAR's in sorted order.
|>
|> This might have been done for other reasons than avoiding side-effect.
|> For example, to avoid having two different sort algorithms, or even
|> because it didn't occur to him how to write the algorithm without
|> doing indexed access.
|>
| Another possible reason is to avoid a certain kind of degenerate cache/
| paging behavior. If you sort a long, randomly ordered list by
| changing cdr's, merely traversing the resulting list cells makes for a
| terrible memory access pattern.
I think there are two many `if's in reasoning about memory access
patterns. You will traverse the list to get it into the array in the
first place, and there may be repeated traversals immediately , on the
resulting list.
But I notice you have made this point in the past, when citing
your paper which exhibited a SHUFFLE algorithm which worked on cons
cells. I could not look it up, perhaps you could post a brief
description of how it worked here?
I agree. There may not be many cases when it would benefit.
On the random permuter, I'm really very sorry, but I have no access to
the original lisp. Only C. At least this is a fairly literal
translation of the lisp. Here, rand31() is a random number in [0 ..
2^31 - 1]. I apologize for
// this is a provably random permuter Call initially with tail=NULL.
NODE *permutation(NODE *p, int len, NODE *tail)
{
NODE *e, *l1, *l2, *next;
unsigned int l1_len, l2_len;
unsigned long r;
if (!p) return tail;
for (r = rand31() % len, e = p; r; --r) e = e->left;
for (r = 1, l1 = l2 = NULL, l1_len = l2_len = 0; p != NULL; p = next)
{
next = p->left;
if (r == 1) r = rand31() | (1 << 31); // random bit generator
if (p != e) {
if (r & 1) { p->left = l1; l1 = p; ++l1_len; }
else { p->left = l2; l2 = p; ++l2_len; }
r >>= 1; // set up for next random bit
}
}
e->left = permutation(l1, l1_len, permutation(l2, l2_len, tail));
return(e);
}
.
- References:
- Re: the sort function in lisp (destructive)
- From: Alex Mizrahi
- Re: the sort function in lisp (destructive)
- From: Rainer Joswig
- Re: the sort function in lisp (destructive)
- From: John Thingstad
- Re: the sort function in lisp (destructive)
- From: Gene
- Re: the sort function in lisp (destructive)
- From: Madhu
- Re: the sort function in lisp (destructive)
- Prev by Date: Re: soft cache
- Next by Date: Re: #;
- Previous by thread: Re: the sort function in lisp (destructive)
- Next by thread: Re: the sort function in lisp (destructive)
- Index(es):
Relevant Pages
|