forked from riya2001-cloud/java-hacktober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble_sort.clj
48 lines (44 loc) · 1.54 KB
/
bubble_sort.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(ns clojure.bubble-sort
"Sorting using the bubble sort algorithm.")
(defn bubblify
"Bubbles the given values."
[vals]
(-> (fn [bubble-vals next-val]
(cond
(empty? bubble-vals) [next-val]
(> next-val (peek bubble-vals)) (conj bubble-vals next-val)
:else (conj (pop bubble-vals) next-val (peek bubble-vals))))
(reduce [] vals)))
(defn rearrange
"Rearranges the given values using the bubble sort method."
[sorted-vals vals]
(if (empty? vals)
sorted-vals
(let [bubbled-vals (bubblify vals)
last-val (peek bubbled-vals)
not-last-vals (pop bubbled-vals)]
(rearrange (cons last-val sorted-vals) not-last-vals))))
(defn sort
"Sorts the given values using the bubble sort algorithm.
Example:
|----------------+----------------|
| sorted | unsorted |
|----------------+----------------|
| [ ] | [ 2- 4- 1 3 ] |
| [ ] | [ 2 4- 1- 3 ] |
| [ ] | [ 2 1 4- 3-] |
| [ ] | [ 2 1 3 4*] |
|----------------+----------------|
| [ 4 ] | [ 2- 1- 3 ] |
| [ 4 ] | [ 1 2- 3-] |
| [ 4 ] | [ 1 2 3*] |
|----------------+----------------|
| [ 3 4 ] | [ 1- 2-] |
| [ 3 4 ] | [ 1 2*] |
|----------------+----------------|
| [2 3 4 ] | [ 1*] |
|----------------+----------------|
| [ 1 2 3 4 ] | [ ] |
|----------------+----------------|"
[vals]
(rearrange [] vals))