From 1b43b214984d73e562cfc9d8e6e75bc355a118e9 Mon Sep 17 00:00:00 2001 From: Francesco Franco <130352141+Francesco601@users.noreply.github.com> Date: Fri, 19 May 2023 11:24:11 +0200 Subject: [PATCH 1/4] added binary_search.lisp Doesn't seem to be in the list. --- bin_search.lisp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 bin_search.lisp diff --git a/bin_search.lisp b/bin_search.lisp new file mode 100644 index 0000000..76f5580 --- /dev/null +++ b/bin_search.lisp @@ -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)) From 2a9c2c0450dbd4dd631da72b0a0314e34a2bd1f7 Mon Sep 17 00:00:00 2001 From: Francesco Franco <130352141+Francesco601@users.noreply.github.com> Date: Fri, 19 May 2023 11:35:05 +0200 Subject: [PATCH 2/4] added merge sort to PR --- merge_sort.lisp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 merge_sort.lisp diff --git a/merge_sort.lisp b/merge_sort.lisp new file mode 100644 index 0000000..1e1164e --- /dev/null +++ b/merge_sort.lisp @@ -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)))))))) From 591ff31d3bcf32d7d3b41c420b86c1515bf0f1ca Mon Sep 17 00:00:00 2001 From: Francesco Franco <130352141+Francesco601@users.noreply.github.com> Date: Fri, 19 May 2023 11:36:21 +0200 Subject: [PATCH 3/4] binary search in LISP --- algorithms/searches/bin_search.lisp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 algorithms/searches/bin_search.lisp diff --git a/algorithms/searches/bin_search.lisp b/algorithms/searches/bin_search.lisp new file mode 100644 index 0000000..76f5580 --- /dev/null +++ b/algorithms/searches/bin_search.lisp @@ -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)) From 19fea0631e6c378007f4c684fce7154a67b4fc87 Mon Sep 17 00:00:00 2001 From: Francesco Franco <130352141+Francesco601@users.noreply.github.com> Date: Mon, 22 May 2023 11:05:00 +0200 Subject: [PATCH 4/4] adding selection sort, insertion sort and shell Adding selection sort, insertion sort and shell short to implementation of algorithms in LISP --- algorithms/sorting/insertion_sort.lisp | 23 +++++++++++++++++++++ algorithms/sorting/selection_sort.lisp | 27 +++++++++++++++++++++++++ algorithms/sorting/shell_sort.lisp | 28 ++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 algorithms/sorting/insertion_sort.lisp create mode 100644 algorithms/sorting/selection_sort.lisp create mode 100644 algorithms/sorting/shell_sort.lisp diff --git a/algorithms/sorting/insertion_sort.lisp b/algorithms/sorting/insertion_sort.lisp new file mode 100644 index 0000000..a9db3f3 --- /dev/null +++ b/algorithms/sorting/insertion_sort.lisp @@ -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))) + diff --git a/algorithms/sorting/selection_sort.lisp b/algorithms/sorting/selection_sort.lisp new file mode 100644 index 0000000..1f25a42 --- /dev/null +++ b/algorithms/sorting/selection_sort.lisp @@ -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))) + diff --git a/algorithms/sorting/shell_sort.lisp b/algorithms/sorting/shell_sort.lisp new file mode 100644 index 0000000..bcb1d2b --- /dev/null +++ b/algorithms/sorting/shell_sort.lisp @@ -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))