Skip to content

Added Lisp implementation for bubble sort #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ indent_size = 2
indent_style = space
indent_size = 2

# Lisp
[*.lisp]
indent_style = space
indent_size = 2

# Matlab
[*.m]
indent_style = space
Expand Down
3 changes: 3 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,7 @@ Michal Hanajik
<br>
Bendik Samseth
<br>
Trashtalk
<br>
Cyrus Burt

4 changes: 4 additions & 0 deletions book.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@
{
"lang": "php",
"name": "PHP"
},
{
"lang": "lisp",
"name": "Lisp"
},
{
"lang": "nim",
Expand Down
4 changes: 4 additions & 0 deletions contents/bubble_sort/bubble_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
[import:1-11, lang:"crystal"](code/crystal/bubble.cr)
{% sample lang="php" %}
[import:3-15, lang:"php"](code/php/bubble_sort.php)
{% sample lang="lisp" %}
[import:3-28, lang:"lisp"](code/lisp/bubble_sort.lisp)
{% endmethod %}

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

<script>
Expand Down
33 changes: 33 additions & 0 deletions contents/bubble_sort/code/lisp/bubble_sort.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
;;;; Bubble sort implementation

(defun bubble-up (list)
(if
(< (length list) 2)
list
(if
(> (first list) (second list))
(cons
(second list)
(bubble-up
(cons
(first list)
(rest (rest list)))))
(cons
(first list)
(bubble-up
(rest list))))))

(defun bubble-sort (list)
(if
(< (length list) 2)
list
(let* ((new-list (bubble-up list)))
(append
(bubble-sort (butlast new-list))
(last new-list)))))

;; The built-in sort: (sort (list 5 4 3 2 1) #'<)
(print
(bubble-sort (list 5 4 3 2 1)))
(print
(bubble-sort (list 1 2 3 3 2 1)))