all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Emanuel Berg <incal@dataswamp.org>
To: emacs-devel@gnu.org
Subject: Re: Add seq-shuffle
Date: Wed, 18 Sep 2024 03:27:55 +0200	[thread overview]
Message-ID: <8734lxpx7o.fsf@dataswamp.org> (raw)
In-Reply-To: 87a5g62kkh.fsf@posteo.net

Philip Kaludercic wrote:

>> That's a neat and concise solution. It seems to produce
>> a decently random sorting, but I'd guess that it may not
>> perform well for large sequences. For example:
>>
>>     (let (comparisons)
>>       (list :result (seq-sort (lambda (a b)
>>                                 (push (cons a b) comparisons)
>>                                 (zerop (random 2)))
>>                               (number-sequence 0 10))
>>             :num-comparisons (length comparisons)))
>>     ;; (:result (6 0 9 1 5 10 8 2 7 3 4) :num-comparisons 26)
>>
>> In testing that expression repeatedly, I see that the
>> number of comparisons varies between about 23 and 26.
>>
>> Thanks for pointing it out, though. It's good to know that
>> it exists and in what circumstances it could be useful.
>
> IIRC the approach is related to the "Naive method" mentioned
> on Wikipedia [0]. I think that this variation of your code
> demonstrates that not all elements are considered equally
> often:
>
>   (let ((comparisons '()))
>     (list :result
>           (seq-sort (lambda (a b)
>                               (cl-incf (alist-get a comparisons 0))
>                               (cl-incf (alist-get b comparisons 0))
>                               (zerop (random 2)))
>                             (number-sequence 0 10))
>           :num-comparisons
>           (seq-sort (lambda (x y)
>                       (< (cdr x) (cdr y)))
>                     comparisons)))
>   ;; (:result
>   ;;  (9 0 1 7 4 10 5 6 8 3 2)
>   ;;  :num-comparisons
>   ;;  ((0 . 2) (10 . 3) (8 . 3) (9 . 4) (7 . 4) (6 . 4) (4 . 5) (2 . 5) (3 . 7) (1 . 7) (5 . 8)))

I think it is as random as `random', as for the exact
execution of the algorithm one should examine 'seq-sort'.

Natural thing would just be to go from one end to the other,
but maybe they do some optimizations.

> While acceptable as a personal hack

Very generous of you, noble sir!

-- 
underground experts united
https://dataswamp.org/~incal




  parent reply	other threads:[~2024-09-18  1:27 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-14  7:48 Add seq-shuffle Hugo Thunnissen
2024-09-14  8:42 ` Philip Kaludercic
2024-09-16  5:08   ` Emanuel Berg
2024-09-16 19:17     ` Adam Porter
2024-09-17  4:09       ` Emanuel Berg
2024-09-17 18:37       ` Philip Kaludercic
2024-09-17 22:26         ` Adam Porter
2024-09-18  1:27         ` Emanuel Berg [this message]
2024-09-17 18:53     ` Yuri Khan
2024-09-15  1:44 ` Adam Porter
2024-09-15  6:27   ` Eli Zaretskii
2024-09-15  6:45     ` Adam Porter
2024-09-15 11:58       ` Stefan Kangas
2024-09-15  7:48   ` Hugo Thunnissen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8734lxpx7o.fsf@dataswamp.org \
    --to=incal@dataswamp.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.