String Sort - LISP

Program:

Download Runnable Code

;;BUBBLESORT (STRING)

(defun breadn(n)
(setf arr (make-array n))
(format t "Enter the strings:~&")
(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)
)

(defun bsortn ( n )
    (breadn n);;(bprintn n)
    (do (( i 1 (+ i 1)))
    ((= i n))
    (setf k (- n 1))
    (do((j 0 (+ j 1    )))
    ((= j k))
    (setf m (+ j 1))
    (if (string> (aref arr j) (aref arr m))
        (swap j m))
))
    (bprintn n)
)
Output:
Break 2 [4]> (load 'bbs.lsp)
;; Loading file bbs.lsp ...
;; Loaded file bbs.lsp
T
Break 2 [4]> (bsortn 5)
Enter the strings:
orange
guava
apple
banana
mango

APPLE
BANANA
GUAVA
MANGO
ORANGE
T
Break 2 [4]>

Download

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...