Skip to content

Commit c763d0d

Browse files
Trashtalk217jiegillet
authored andcommitted
Lisp bogo sort (#512)
* First working version of bogo sort in lisp * FIxed the sortedp to see two equal items as sorted. * Added lisp implementation to the .md file * Cleaned up the shuffle method
1 parent 2eb30cb commit c763d0d

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

contents/bogo_sort/bogo_sort.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ In code, it looks something like this:
5555
[import:3-8, lang:"lisp"](code/racket/bogo_sort.rkt)
5656
{% sample lang="st" %}
5757
[import:2-6, lang:"st"](code/smalltalk/bogosort.st)
58+
{% sample lang="lisp" %}
59+
[import:20-24, lang:"lisp"](code/lisp/bogo-sort.lisp)
5860
{% endmethod %}
5961

6062
That's it.
@@ -109,6 +111,8 @@ We are done here!
109111
[import, lang:"lisp"](code/racket/bogo_sort.rkt)
110112
{% sample lang="st" %}
111113
[import, lang:"st"](code/smalltalk/bogosort.st)
114+
{% sample lang="lisp" %}
115+
[import, lang:"lisp"](code/lisp/bogo-sort.lisp)
112116
{% endmethod %}
113117

114118

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
;;;; Bogo sort implementation
2+
3+
(defun sortedp (list)
4+
"Checks if a list is sorted"
5+
(if (< (length list) 2)
6+
t
7+
(if (<= (first list) (second list))
8+
(sortedp (rest list))
9+
nil)))
10+
11+
(defun shuffle (list)
12+
"Returns a shuffled list using the Fisher-Yates method"
13+
(loop for i from (1- (length list)) downto 0
14+
do
15+
(rotatef
16+
(nth i list)
17+
(nth (random (1+ i)) list))
18+
finally (return list)))
19+
20+
(defun bogo-sort (list)
21+
"Sorts a given list (eventually)"
22+
(if (sortedp list)
23+
list
24+
(bogo-sort (shuffle list))))
25+
26+
(print (bogo-sort (list 1 3 2 4)))

0 commit comments

Comments
 (0)