Re: qsort optimized for brevity




"Marc Battyani" <Marc.Battyani@xxxxxxxxxxxxxxxxxx> wrote
> <nallen05@xxxxxxxxx> wrote
> > noone said it had to be fast right? ; - )
> >
> > (defun qsort (list &optional 2nd)
> > (if (null list)
> > 2nd
> > (let ((max (eval (cons 'max list))))
>
> The eval will be really slow and the evaluation of max will crash if list is too big.
> You should use (reduce 'max list) instead.
>
> But don't worry it will be slow enough anyway ;-)

In fact I would rewrite it that way:
(note that it's not a quick sort anymore)

(defun qsort (list)
(when list
(let ((max (reduce 'max list)))
(cons max (qsort (remove max list :count 1))))))

(qsort '(1 5 3 2 4 6 8 4 5 6 1 5))
(8 6 6 5 5 5 4 4 3 2 1 1)

Marc


.



Relevant Pages

  • Re: qsort optimized for brevity
    ... The eval will be really slow and the evaluation of max will crash if list is too big. ... Marc ... Prev by Date: ...
    (comp.lang.lisp)
  • Default Date Problem
    ... MSACESS.EXE crashes. ... that the old field was corrupted somehow) and to the form, ... with its Datecauses a crash. ... Prev by Date: ...
    (microsoft.public.access.forms)
  • Re: ZX10R
    ... immaculate K1 stuff that Hog fitted after his crash. ... Lozzo ... Prev by Date: ...
    (uk.rec.motorcycles)
  • Re: (i++ * i++)
    ... :>: crash. ... It should generate no overhead but simply allow conforming ... :> implementations to assume that i1 and i2 are distinct objects. ... Otherwise the behavior resulting from that evaluation is "undefined", ...
    (comp.lang.cpp)
  • Re: Attachments
    ... "Brambles" wrote in message ... > I have the same problem.I print for our office records, ... evaluation and in the event of a crash or virus. ...
    (microsoft.public.outlook)