Skip to content

Commit 2226a8c

Browse files
committed
Refactored: 4 to 2 spaces and renamed things.
1 parent 3de5654 commit 2226a8c

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

contents/bubble_sort/bubble_sort.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
4545
{% sample lang="php" %}
4646
[import:3-15, lang:"php"](code/php/bubble_sort.php)
4747
{% sample lang="lisp" %}
48-
[import:3-14, lang:"lisp"](code/lisp/bubble_sort.lisp)
48+
[import:3-15, lang:"lisp"](code/lisp/bubble_sort.lisp)
4949
{% endmethod %}
5050

5151
... And that's it for the simplest bubble sort method.
Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
1-
;;;;Bubble sort implementation
1+
;;;; Bubble sort implementation
22

3-
;;;Swaps to elements in a list (complexity: O(n))
3+
;;; Swaps two elements in a list (complexity: O(n))
44
(defun swap (lst low high)
5-
(let ((list-tail (nthcdr low lst)))
6-
(rotatef (car list-tail) (elt list-tail (- high low)))
7-
lst))
5+
(let ((list-tail (nthcdr low lst)))
6+
(rotatef (car list-tail) (elt list-tail (- high low)))
7+
lst))
88

9-
(defun bubble_sort (lst)
10-
(dotimes (m (- (length lst) 1) lst) ;loop
11-
(dotimes (n (- (length lst) (+ 1 m)) lst) ;loop
12-
(if (> (nth n lst) (nth (+ n 1) lst)) ;if
13-
(swap lst n (+ n 1)) ;then
14-
lst)))) ;else
9+
(defun bubble-sort (lst)
10+
(dotimes (m (- (length lst) 1) lst) ;loop
11+
(dotimes (n (- (length lst) (+ 1 m)) lst) ;loop
12+
(if (> (nth n lst) (nth (+ n 1) lst)) ;if
13+
(swap lst n (+ n 1)) ;then
14+
lst)))) ;else
1515

16-
;;Use of an array insead of a list would be faster
17-
;;The built-in sort is also quicker (sort (list 5 4 3 2 1))
18-
(print (bubble_sort (list 5 4 3 2 1)))
16+
;; Use of an array instead of a list would be faster
17+
(print
18+
(bubble-sort (list 5 4 3 2 1)))
19+
(print
20+
(bubble-sort (list 1 2 3 3 2 1)))
21+
22+
;; The built-in sort is quicker, see the usage below
23+
;; quick test
24+
(assert
25+
(equal (bubble-sort (list 5 4 3 2 1))
26+
(sort '(1 2 3 4 5) #'<)))
27+
28+
(assert
29+
(equal (bubble-sort (list 1 2 3 3 2 1))
30+
(sort '(1 2 3 3 2 1) #'<)))

0 commit comments

Comments
 (0)