Skip to content

Commit e5ddeba

Browse files
Trashtalk217jiegillet
authored andcommitted
Added Lisp implementation for bubble sort (#348)
* Bubble sort implementation for Lisp * Adding Trashtalk to contributors file * Add EditorConfig rule for Lisp. * Add Lisp to book.json
1 parent cf487dd commit e5ddeba

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed

.editorconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ indent_size = 2
7575
indent_style = space
7676
indent_size = 2
7777

78+
# Lisp
79+
[*.lisp]
80+
indent_style = space
81+
indent_size = 2
82+
7883
# Matlab
7984
[*.m]
8085
indent_style = space

CONTRIBUTORS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,7 @@ Michal Hanajik
5858
<br>
5959
Bendik Samseth
6060
<br>
61+
Trashtalk
62+
<br>
6163
Cyrus Burt
64+

book.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@
127127
{
128128
"lang": "php",
129129
"name": "PHP"
130+
},
131+
{
132+
"lang": "lisp",
133+
"name": "Lisp"
130134
},
131135
{
132136
"lang": "nim",

contents/bubble_sort/bubble_sort.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
4444
[import:1-11, lang:"crystal"](code/crystal/bubble.cr)
4545
{% sample lang="php" %}
4646
[import:3-15, lang:"php"](code/php/bubble_sort.php)
47+
{% sample lang="lisp" %}
48+
[import:3-28, lang:"lisp"](code/lisp/bubble_sort.lisp)
4749
{% endmethod %}
4850

4951
... And that's it for the simplest bubble sort method.
@@ -93,6 +95,8 @@ Trust me, there are plenty of more complicated algorithms that do precisely the
9395
[import, lang:"crystal"](code/crystal/bubble.cr)
9496
{% sample lang="php" %}
9597
[import, lang:"php"](code/php/bubble_sort.php)
98+
{% sample lang="lisp" %}
99+
[import, lang:"lisp"](code/lisp/bubble_sort.lisp)
96100
{% endmethod %}
97101

98102
<script>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
;;;; Bubble sort implementation
2+
3+
(defun bubble-up (list)
4+
(if
5+
(< (length list) 2)
6+
list
7+
(if
8+
(> (first list) (second list))
9+
(cons
10+
(second list)
11+
(bubble-up
12+
(cons
13+
(first list)
14+
(rest (rest list)))))
15+
(cons
16+
(first list)
17+
(bubble-up
18+
(rest list))))))
19+
20+
(defun bubble-sort (list)
21+
(if
22+
(< (length list) 2)
23+
list
24+
(let* ((new-list (bubble-up list)))
25+
(append
26+
(bubble-sort (butlast new-list))
27+
(last new-list)))))
28+
29+
;; The built-in sort: (sort (list 5 4 3 2 1) #'<)
30+
(print
31+
(bubble-sort (list 5 4 3 2 1)))
32+
(print
33+
(bubble-sort (list 1 2 3 3 2 1)))

0 commit comments

Comments
 (0)