-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection-sort.lisp
46 lines (43 loc) · 1.11 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
(defun max-element (lst element)
(if (and (true-listp lst) (> (length lst) 0))
(if (< element (car lst))
(max-element (cdr lst) (car lst))
(max-element (cdr lst) element)
)
element
)
)
(defun remove-by-value (result lst element)
(if (> (length lst) 0)
(if (= element (car lst))
(append result (cdr lst))
(remove-by-value (append result (list (car lst))) (cdr lst) element)
)
nil
)
)
(defun selection-sort-internal (sorted-lst raw-lst)
(if (> (length raw-lst) 0)
(selection-sort-internal
(append
sorted-lst
(list
(max-element
(cdr raw-lst)
(car raw-lst)
)
)
)
(remove-by-value
'()
raw-lst
(max-element
(cdr raw-lst)
(car raw-lst)
)
)
)
sorted-lst
)
)
(selection-sort-internal '() '(1 2 3 4 5))