Jump to content

Quicksort


CALCAD

Recommended Posts

Here is an Autolisp implementation of the Quicksort algorithm. It's been a few years, but I think I adapted this code from a C language example. I've used many different sorting routines, but Quicksort is the best for general sorts.

 

(defun qsort (inlist / pivot dpivot ppivot index list1 list2 )
     (if (<= (length inlist) 1)
        inlist
        (progn
           (setq ppivot (/ (length inlist) 2))
           (setq pivot (cdr (nth ppivot inlist)))
           (setq dpivot (nth ppivot inlist))
           (setq list1 ())
           (setq list2 ())
           (setq index 0)
           (while (< index (length inlist))
              (if (/= index ppivot)
                 (if (< (cdr (nth index inlist)) pivot)
                    (setq list1 (append list1 (list (nth index inlist))))
                    (setq list2 (append list2 (list (nth index inlist))))
                 )
              )
              (setq index (+ index 1))
           )
           (setq list1 (qsort list1))
           (setq list2 (qsort list2))
           (setq inlist (append list1 (list dpivot) list2))
        )
     )
)

Link to comment
Share on other sites

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...