-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection_sort.lisp
27 lines (24 loc) · 1.06 KB
/
selection_sort.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
;LISP implementation of selection sort
(defun selection-sort (sequence)
(let ((end (length sequence)))
(labels ((position-of-minimum (index minimum result)
(if (= index end)
result
(let ((x (elt sequence index)))
(if (< x minimum)
(position-of-minimum (1+ index) x index)
(position-of-minimum (1+ index) minimum result)))))
(select-and-swap (start)
(if (= start end)
(values)
(let* ((x (elt sequence start))
(position (position-of-minimum start x start)))
(if (= position start)
(select-and-swap (1+ start))
(progn
(setf (elt sequence start) (elt sequence position)
(elt sequence position) x)
(select-and-swap (1+ start))))))))
(unless (< end 2)
(select-and-swap 0))
sequence)))