Jump to content

Recommended Posts

Posted
24 minutes ago, PGia said:

I've tried Bubblesort and it seems to work well. But won't the recursive call to Bubblepass be problematic with very long lists?

 

I would strongly advise against using that bubblesort function, it is horribly inefficient.

(vl-sort)/(vl-sort-i) both use a version of quicksort (one of the fastest sorting algorithms), implemented in C++; as such, they will always outperform any other sorting algorithm implemented using the LISP API.

 

24 minutes ago, PGia said:

The only drawback is that it always returns the full list of indices even though the user might only need a smaller number of elements.

 

If the subset is known prior to sorting then sort only the subset (since the sort is the heaviest operation); else you have no choice but to take the subset following the sort.

Posted (edited)

You should have said at the 1st post want to sort up to 5 items deep in a list. 

 

I had this task for something else I did. Hopefully this is a dummy of what your after. it is amost instant to make 10000 items and sort them. I apoligise I forget where I got the complex sort from regarding authors name.

; sort on up to 5 items in sub lists
; By AlanH

(defun compare-elements (a b sortspec)
((lambda (x y test)
(if (and (equal x y ) (cdr sortspec))
  (compare-elements a b (cdr sortspec))
  (apply test (list x y))
)
)
(nth (cdar sortspec) a)
(nth (cdar sortspec) b)
(caar sortspec)
)
)

(defun complex-sort (alst sortspec)
  (vl-sort alst
  '(lambda (a b)
  (compare-elements a b sortspec)
  )
  )
)

;; Rand  -  Lee Mac
;; PRNG implementing a linear congruential generator with
;; parameters derived from the book 'Numerical Recipes'

(defun LM:rand ( / a c m )
    (setq m   4294967296.0
          a   1664525.0
          c   1013904223.0
          $xn (rem (+ c (* a (cond ($xn) ((getvar 'date))))) m)
    )
    (/ $xn m)
)

;; Random in Range  -  Lee Mac
;; Returns a pseudo-random integral number in a given range (inclusive)

(defun LM:randrange ( a b )
    (+ (min a b) (fix (* (LM:rand) (1+ (abs (- a b))))))
)

(defun wow ( / )
(setq lst '())
(repeat 10000
(setq v1 (atoi (rtos (LM:randrange 1 1000 ) 2 0)))
(setq v2 (atof (rtos (LM:randrange 1 1000 ) 2 3)))
(setq v3 (atof (rtos (LM:randrange 1 1000 ) 2 3)))
(setq v4 (atof (rtos (LM:randrange 1 1000 ) 2 3)))
(setq v5 (atof (rtos (LM:randrange 1 1000 ) 2 3)))
(setq lst (cons (list v1 v2 v3 v4 v5) lst))
)
(setq numcols (length (car lst)))
(cond
  ((= numcols 1)(setq lst (complex-sort lst '((< . 0)))))
  ((= numcols  2)(setq lst (complex-sort lst '((< . 0) (< . 1)))))
  ((= numcols  3)(setq lst (complex-sort lst '((< . 0) (< . 1) (< . 2)))))
  ((= numcols  4)(setq lst (complex-sort lst '((< . 0) (< . 1) (< . 2)(< . 3)))))
  ((= numcols  5)(setq lst (complex-sort lst '((< . 0) (< . 1) (< . 2)(< . 3)(< . 4)))))
  ((> numcols  5)(setq lst (complex-sort lst '((< . 0) (< . 1) (< . 2)(< . 3)(< . 4)))))
)
(princ)
)
(wow)

 

 

Edited by BIGAL

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...