Search code examples
javaalgorithmintellij-ideaclojurebubble-sort

How to populate an array from size (equal to user input) with random unique numbers? CLOJURE


currently programming the bubblesort algorithm. I found the source code of someone of the web:

Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array which you would like to create: ");

What the application now does, it that it takes the input number of the user. And makes an array of random numbers. Using this array, it applies the Bubblesort algorithm.

Now, I am trying to translate this to Clojure. I know that the user input can be achieved by the functions:

Flush

and

read-line

Apart from that, I don't understand how to get the user-input for required array size, then create an array of random numbers, and then applying bubble sort. So far, this is what I have (Bubblesort found from rosetta website.):

 (ns BubbleSort
  (:import java.util.ArrayList)
  (:import (java.util Date)))

(defn safe-println [& more]
  (.write *out* (str (clojure.string/join " " more) "\n")))


; set a timestamp
(defn restart-profiling []
  (def last-time-stamp (atom (System/nanoTime))
    )
  )

; get ms since last timestamp
(defn get-delta-ms []
  (let [last @last-time-stamp
        current (System/nanoTime)
        ticks (- current last)
        ]

    (restart-profiling)
    (float (/ ticks 1000000)) ; calculate the delta in milliseconds
    )
  )


(defn bubble-sort
  "Sort in-place.
  arr must implement the Java List interface and should support
  random access, e.g. an ArrayList."
  ([arr] (bubble-sort compare arr))
  ([cmp arr]
   (letfn [(swap! [i j]
             (let [t (.get arr i)]
               (doto arr
                 (.set i (.get arr j))
                 (.set j t))))
           (sorter [stop-i]
             (let [changed (atom false)]
               (doseq [i (range stop-i)]
                 (if (pos? (cmp (.get arr i) (.get arr (inc i))))
                   (do
                     (swap! i (inc i))
                     (reset! changed true))))
               @changed))]
     (doseq [stop-i (range (dec (.size arr)) -1 -1)
             :while (sorter stop-i)])
     arr)))

(restart-profiling)
(println (bubble-sort (ArrayList. [10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6


                                   ])))

(safe-println "The serial implementation "(get-delta-ms) "ms")

Needed:

  • user-input
  • creating an array of random number of user-input size

Thanks in advance!

EDIT:

User-input:

    def(input
      (println "Enter the size of the array: ")
    )
    println(read-line)

the input should determine the size of the array.

Remaining part:

Populate array with unique random numbers of the size represented by input.


Solution

  • After you read the input with read-line, you can parse it into an integer with read-string, then use rand to generate random numbers. After that, convert them to a Java array using to-array. The below function will prompt the user for an array size and return an array of random numbers from 0 to 1:

    (defn random-array-prompt []
      (print "Enter array size:")
      (let [n (read-string (read-line))]
        (to-array (repeatedly n rand))))
    

    After that, can call that function in the repl:

    (println (seq (bubble-sort (random-array-prompt))))
    

    This will prompt you for the size and print the sorted sequence.