Jump to content

Recommended Posts

Posted

Hi guys,

Anybody can explain me what's happening here?

 

(setq lst (list 1 5 7 20 4 44 123 15 54 32  7 20 4 44 76 12))
(vl-sort lst
    '(lambda (a b) ( <  (/ a  b) 3)))

(setq lst (list 1 5 7 20 4 44 123 15 54 32  7 20 4 44 76 12))
(vl-sort lst
    '(lambda (a b) ( >  (/ a  b) 3)))

 

I try to understand how vl-sort is working...

Posted

The result is (44 123 54 32 44 76 5 20 4 15 7 20 4 12 1) - i can't see any order in this list....???

Posted

I really don't understand your sorting criteria. You try to sort the numbers as well the result of division between two adjacent items to be smaller than 3?!?

 

The result of such sorting will be influenced by the initial order of the numbers, so is totally unreliable:

Case 1:

(setq lst (list 1 5 7 20 4 44 123 15 54 32 7 20 4 44 76 12))
(vl-sort lst
    '(lambda (a b) ( >  (/ a  b) 3)))
[color=magenta];Result: (44 123 54 32 44 76 5 20 4 15 7 20 4 12 1)[/color]

Case 2:

(setq lst (list 7 20 4 44 76 7 20 4 44 12 123 15 54 1 5 32))
(vl-sort lst
    '(lambda (a b) ( >  (/ a  b) 3)))
[color=magenta];Result: (76 20 44 123 54 4 7 20 4 12 15 32 5 1)[/color]

Posted
Anybody can explain me what's happening here?

 

vl-sort will re-order items in a given list based upon the return of the supplied comparison function for every pair of items tested. If the comparison function returns T for the two arguments, this indicates that the first argument precedes the second argument in the sorted list, and so by comparing all pairs, vl-sort will alter list order accordingly. There are many different algorithms which use this comparison sorting technique (Quick Sort, Merge Sort, Bubble Sort, to name a few); the particular algorithm implemented by the vl-sort function is not documented, but I would guess that it likely uses a Quick Sort algorithm.

 

You may find this thread of particular interest when understanding the inner-workings of a sorting algorithm.

 

What result are you looking to obtain from your function?

Posted

In fact, i reached this situation by mistake... and this is why i was trying to understand the algorithm inside vl-sort...thanks, Lee! :)

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