Skip to content

Instantly share code, notes, and snippets.

@bnyeggen
Created November 6, 2012 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnyeggen/4022149 to your computer and use it in GitHub Desktop.
Save bnyeggen/4022149 to your computer and use it in GitHub Desktop.
Rational bozo sort
(defn rational-bozo-sort
"Choose, compare & if necessary exchange random indices n times, using the
implicit ordering given by the lookup function"
[lookup s n]
(let [a (to-array s)
c (alength a)]
(dotimes [_ n]
(let [idx1 (rand-int c)
idx2 (rand-int idx1)
v1 (aget a idx1)
v2 (aget a idx2)]
(when (< (lookup v1) (lookup v2))
(aset a idx1 v2)
(aset a idx2 v1))))
(vec a)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment