52 lines
1.4 KiB
Plaintext
52 lines
1.4 KiB
Plaintext
(import agent "vendor:agent")
|
|
|
|
(defn find-match [str str pattern str] i32
|
|
(dotimes [i (length str)]
|
|
(let [matched true]
|
|
(dotimes [j (length pattern)]
|
|
(when (!= (at str (+ i j)) (at pattern j))
|
|
(set matched false)))
|
|
(when matched
|
|
(return i))))
|
|
-1)
|
|
|
|
(defn selection-sort [coll [$t]] ()
|
|
{:where (is-ordered $t)}
|
|
(let [len (length coll)]
|
|
(dotimes [i len]
|
|
(let [min-val i]
|
|
(dotimes [j (+ i 1) len]
|
|
(when (< (at coll j) (at coll min-val))
|
|
(set min-val j)))
|
|
(let [tmp (at coll min-val)]
|
|
(set (at coll min-val) (at coll i))
|
|
(set (at coll i) tmp))))))
|
|
|
|
(defn insertion-sort [coll [$t]] ()
|
|
{:where (is-ordered $t)}
|
|
(let [i 1
|
|
length (length coll)]
|
|
(while (and (< i length))
|
|
(let [j i]
|
|
(while (and (> j 0)
|
|
(< (at coll j) (at coll (dec j))))
|
|
(let [temp (at coll j)]
|
|
(set (at coll j) (at coll (dec j)))
|
|
(set (at coll (dec j)) temp))
|
|
(-- j)))
|
|
(++ i))))
|
|
|
|
(defn main [] i32 0)
|
|
|
|
(comment
|
|
(insertion-sort [\I \N \S \E \R \T \I \O \N \S \O \R \T])
|
|
(insertion-sort (slice [6 2 4 9 1 9 4 5] 0 8))
|
|
(let [str (bytes "INSERTIONSORT")]
|
|
(insertion-sort str)
|
|
(println str))
|
|
(let [str (bytes "SELECTIONSORT")]
|
|
(selection-sort str)
|
|
(println str))
|
|
(find-match "aababba" "abba")
|
|
:-)
|