Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding LISP programs not in list #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions algorithms/searches/bin_search.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(defvar searchspace '(1 5 10 15 20 25))
(defvar target 20)
(defvar lo 0) (defvar high (- (list-length searchspace) 1)) (defvar mid)
(defun bin-search (listToSearch)
(setq mid (round (/ (+ lo high) 2)))
(if (eq (nth mid listToSearch) target)
(return-from bin-search mid)
(if (> (nth mid listToSearch) target)
(setq high (- mid 1))
(setq lo (+ mid 1))
)
)
(bin-search listToSearch)
)

(print (bin-search searchspace))
23 changes: 23 additions & 0 deletions algorithms/sorting/insertion_sort.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

; Lisp implementation of insertion sort algorithm
(defun insertion-sort (sequence)
(let ((end (length sequence)))
(labels ((insert (x index)
(if (minusp index)
(setf (elt sequence (1+ index)) x)
(let ((y (elt sequence index)))
(if (< x y)
(progn
(setf (elt sequence (1+ index)) y)
(insert x (1- index)))
(setf (elt sequence (1+ index)) x)))))
(repeat-insertion (start)
(if (= start end)
(values)
(progn
(insert (elt sequence start) (1- start))
(repeat-insertion (1+ start))))))
(unless (< end 2)
(repeat-insertion 1))
sequence)))

27 changes: 27 additions & 0 deletions algorithms/sorting/selection_sort.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

;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)))

28 changes: 28 additions & 0 deletions algorithms/sorting/shell_sort.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(defun shellsort (gap-sequence-fn sequence)
(let ((len (length sequence)))
(unless (< len 2)
(mapc (lambda (gap)
(labels ((insert (x index)
(if (minusp index)
(setf (elt sequence (+ index gap)) x)
(let ((y (elt sequence index)))
(if (< x y)
(progn
(setf (elt sequence (+ index gap)) y)
(insert x (- index gap)))
(setf (elt sequence (+ index gap)) x)))))
(repeat-insertion (index)
(if (>= index len)
(values)
(progn
(insert (elt sequence index) (- index gap))
(repeat-insertion (+ index gap)))))
(h-sorting (h)
(if (zerop h)
(values)
(progn
(repeat-insertion (1- (+ h gap)))
(h-sorting (1- h))))))
(h-sorting gap)))
(funcall gap-sequence-fn len)))
sequence))
16 changes: 16 additions & 0 deletions bin_search.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(defvar searchspace '(1 5 10 15 20 25))
(defvar target 20)
(defvar lo 0) (defvar high (- (list-length searchspace) 1)) (defvar mid)
(defun bin-search (listToSearch)
(setq mid (round (/ (+ lo high) 2)))
(if (eq (nth mid listToSearch) target)
(return-from bin-search mid)
(if (> (nth mid listToSearch) target)
(setq high (- mid 1))
(setq lo (+ mid 1))
)
)
(bin-search listToSearch)
)

(print (bin-search searchspace))
16 changes: 16 additions & 0 deletions merge_sort.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

(defun merge-sort (list)
(labels ((merge-aux (f s)
(cond
((null f) s)
((null s) f)
((< (first f) (first s)) (list* (first f) (merge-aux (rest f) s)))
((> (first f) (first s)) (list* (first f) (merge-aux f (rest s))))
((= (first f) (first s)) (list* (first f)
(first s)
(merge-aux (rest f) (rest s)))))))
(let ((len (list-length list)))
(if (= len 1)
list
(merge-aux (merge-sort (subseq list 0 (ceiling len 2)))
(merge-sort (subseq list (ceiling len 2))))))))