Bubble Sort in Clojure

Published on 28 April 2022 (Updated: 25 January 2023)

Bubble Sort in Clojure

Welcome to the Bubble Sort in Clojure page! Here, you’ll find the source code for this program as well as a description of how the program works.

Current Solution

(ns bubble-sort
(defn- bubble [ys x]
  (if-let [y (peek ys)]
    (if (> y x)
      (conj (pop ys) x y)
      (conj ys x))

(defn bubble-sort [xs]
  (let [ys (reduce bubble [] xs)]
    (if (= xs ys)
      (recur ys))))

(defn- convert-to-int-array [string]
  (->> (clojure.string/split string #", " )
       (map #(Integer/parseInt %))))

(defn- convert-to-string [int-array]
  (clojure.string/join ", " int-array))

(defn- print-bubble-sort [string]   
        (convert-to-int-array string)))))

(defn- is-valid-input [args]
    (not= (count args) 0) 
    (not= (first args) "")
    (> (count (convert-to-int-array (first args))) 1)

(defn- print-error []
  (println "Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\""))

(defn main [args]
    (if (is-valid-input args) 
      (print-bubble-sort (first args)) 
    (catch Exception e (print-error))))

(main *command-line-args*)

Bubble Sort in Clojure was written by:

If you see anything you’d like to change or update, please consider contributing.

Note: The solution shown above is the current solution in the Sample Programs repository as of Oct 19 2019 23:52:25. The solution was first committed on Oct 02 2019 16:17:26. As a result, documentation below may be outdated.

How to Implement the Solution

No ‘How to Implement the Solution’ section available. Please consider contributing.

How to Run the Solution

No ‘How to Run the Solution’ section available. Please consider contributing.