### Author Topic: Sort list of numbers (vl-sort not allowed)  (Read 19767 times)

0 Members and 1 Guest are viewing this topic.

#### xiaxiang

• Guest
##### Re: Sort list of numbers (vl-sort not allowed)
« Reply #45 on: June 24, 2012, 09:20:58 PM »
This is the lisp file. includes  my code,gile's ,Stefan's,ireb's ,ElpanovEvgeniy's code.
I compiled this file,then run the test.
Nicely work,HighflyingBird
Thank you!

#### chlh_jd

• Guest
##### Re: Sort list of numbers (vl-sort not allowed)
« Reply #46 on: October 17, 2014, 12:10:53 PM »
Hi, Gile
After revisit this site , I found a bug in the quicksort function you post .
Code: [Select]
` (quicksort '(6 5 4 3 3 2 1)) ;;--> '(1 2 3 3 3 4 5 6)`It more out a number 3 .

#### Lee Mac

• Seagull
• Posts: 12313
• London, England
##### Re: Sort list of numbers (vl-sort not allowed)
« Reply #47 on: October 17, 2014, 06:27:10 PM »
Hi, Gile
After revisit this site , I found a bug in the quicksort function you post .
Code: [Select]
` (quicksort '(6 5 4 3 3 2 1)) ;;--> '(1 2 3 3 3 4 5 6)`It more out a number 3 .
Change:
Code: [Select]
`((< ele (car lst)) (left ele (cdr lst)))`to:
Code: [Select]
`((<= ele (car lst)) (left ele (cdr lst)))`

#### Lee Mac

• Seagull
• Posts: 12313
• London, England
##### Re: Sort list of numbers (vl-sort not allowed)
« Reply #48 on: October 17, 2014, 07:43:38 PM »
FWIW, the function could also be written:
Code - Auto/Visual Lisp: [Select]
1. (defun qs ( l ) (if l (fn (car l) (cdr l))))
2. (defun fn ( x l / m )
3.     (setq l (vl-remove-if '(lambda ( y ) (if (< x y) (setq m (cons y m)))) l))
4.     (append (qs l) (cons x (qs (reverse m))))
5. )
Code - Auto/Visual Lisp: [Select]
1. _\$ (qs '(6 5 4 3 3 2 1))
2. (1 2 3 3 4 5 6)

#### chlh_jd

• Guest
##### Re: Sort list of numbers (vl-sort not allowed)
« Reply #49 on: October 18, 2014, 01:42:09 PM »
Thanks Lee Mac  , thank you a lot .

#### Lee Mac

• Seagull
• Posts: 12313
• London, England
##### Re: Sort list of numbers (vl-sort not allowed)
« Reply #50 on: October 18, 2014, 01:43:35 PM »
Thanks Lee Mac  , thank you a lot .

You're most welcome chlh_jd