Bubblesort - LISP

;;BUBBLESORT
(defun bsortn (n)
    (format t "    <<<BUBBLESORT FOR ~D NUMBERS>>>~&" n)
    (breadn n)
    (do (( i 0 (+ i 1))) ((= i (- n 1)))
        (do ((j 0 (+ j 1))) ((= j (- (- n i) 1)))
            (if (> (aref arr j) (aref arr (+ j 1)))
                (swap j (+ j 1))
            )
        )
    )
    (bprintn n)
)
(defun breadn(n)
    (setf arr (make-array n))
    (format t "Enter the numbers:~&")
    (dotimes (x n t)
        (setf (aref arr x) (read) )
    )
)
(defun bprintn(n)
    (dotimes(x n t)
        (print (aref arr x) )
    )
)
(defun swap(x y)
    ( setf temp (aref arr x) )
    ( setf (aref arr x) (aref arr y) )
    ( setf (aref arr y) temp )
)

Output:
Break 1 [2]> (load 'b.lsp)
;; Loading file b.lsp ...
;; Loaded file b.lsp
T
Break 1 [2]> (bsortn 5)
<<<BUBBLESORT FOR 5 NUMBERS>>>
Enter the numbers:
5
4
3
2
1
1
2
3
4
5
T
Break 1 [2]>            http://www.2k8618.blogspot.com/


3 comments:

Related Posts Plugin for WordPress, Blogger...