如何使用随机唯一数字从大小(等于用户输入)填充数组?编译器

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

目前正在编写冒泡排序算法。 我找到了网上某人的源代码:

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

应用程序现在做什么,它接受用户输入的号码。并生成一个随机数数组。使用此数组,它应用了 Bubblesort 算法。

现在,我正在尝试将其翻译成 Clojure。 我知道用户输入可以通过函数实现:

Flush

read-line

除此之外,我不明白如何获取所需数组大小的用户输入,然后创建一个随机数数组,然后应用冒泡排序。 到目前为止,这就是我所拥有的(从 rosetta 网站上找到的 Bubblesort。):

 (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")

需要:

提前致谢!

编辑:

用户输入:

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

输入应确定数组的大小。

剩余部分:

用输入表示的大小的唯一随机数填充数组。

read-line读取输入后,可以用read-string解析成整数,然后用rand生成随机数。之后,使用 to-array 将它们转换为 Java 数组。下面的函数将提示用户输入数组大小和 return 一个从 0 到 1 的随机数数组:

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

之后,可以在repl中调用那个函数:

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

这将提示您输入大小并打印排序序列。