From ba85d6137defa4d2f79b260f5bf8dd721b452e78 Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Tue, 7 Aug 2018 14:03:28 +0200 Subject: [PATCH 01/12] Adding Trashtalk to contributors file --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index f188deacc..fddd93812 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -57,3 +57,4 @@ NIFR91 Michal Hanajik
Bendik Samseth +Trashtalk From 492826d6d6ea3cd6231fe4525934106500eeac50 Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Thu, 9 Aug 2018 12:09:22 +0200 Subject: [PATCH 02/12] Bubble sort implementation for Lisp --- CONTRIBUTORS.md | 1 + .../bubble_sort/code/lisp/bubble_sort.lisp | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 contents/bubble_sort/code/lisp/bubble_sort.lisp diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index fddd93812..77269e07c 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -58,3 +58,4 @@ Michal Hanajik
Bendik Samseth Trashtalk +Trashtalk diff --git a/contents/bubble_sort/code/lisp/bubble_sort.lisp b/contents/bubble_sort/code/lisp/bubble_sort.lisp new file mode 100644 index 000000000..0c4eb860f --- /dev/null +++ b/contents/bubble_sort/code/lisp/bubble_sort.lisp @@ -0,0 +1,18 @@ +;;;;Bubble sort implementation + +;;;Swaps to elements in a list (complexity: O(n)) +(defun swap (lst low high) + (let ((list-tail (nthcdr low lst))) + (rotatef (car list-tail) (elt list-tail (- high low))) + lst)) + +(defun bubble_sort (lst) + (dotimes (m (- (length lst) 1) lst) ;loop + (dotimes (n (- (length lst) (+ 1 m)) lst) ;loop + (if (> (nth n lst) (nth (+ n 1) lst)) ;if + (swap lst n (+ n 1)) ;then + lst)))) ;else + +;;Use of an array insead of a list would be faster +;;The built-in sort is also quicker (sort (list 5 4 3 2 1)) +(print (bubble_sort (list 5 4 3 2 1))) \ No newline at end of file From e70f287b794eff9f95579390c67c178aa88d7098 Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Thu, 9 Aug 2018 14:18:49 -0400 Subject: [PATCH 03/12] Add bubble sort implementation in Lisp to book. --- contents/bubble_sort/bubble_sort.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/contents/bubble_sort/bubble_sort.md b/contents/bubble_sort/bubble_sort.md index 20716a107..423a4404b 100644 --- a/contents/bubble_sort/bubble_sort.md +++ b/contents/bubble_sort/bubble_sort.md @@ -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-14, lang:"lisp"](code/lisp/bubble_sort.lisp) {% endmethod %} ... And that's it for the simplest bubble sort method. @@ -93,6 +95,8 @@ Program.cs [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 %}