Created
November 6, 2025 15:57
-
-
Save theronic/62a03642ffc28f50b8acad22760cb33f to your computer and use it in GitHub Desktop.
Bubble Sort in Clojure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defn bubble-sort-step [coll] | |
| (if-not (seq coll) | |
| coll | |
| (let [[a b & rst] coll] | |
| (if-not b | |
| coll | |
| (lazy-seq | |
| (cons (min a b) | |
| (bubble-sort-step (cons (max a b) rst)))))))) | |
| (defn fixed-point | |
| "Applies f repeatedly to x until the result no longer changes (i.e., f(result) = result)." | |
| [f x] | |
| (loop [current x] | |
| (let [next (f current)] | |
| (if (= next current) | |
| current | |
| (recur next))))) | |
| (comment | |
| (fixed-point bubble-sort-step '(4 3 2 1 5)) => (1 2 3 4 5) | |
| ) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
there are cleaner ways to do this with reduce, and peek