-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_srch_with_sorting.lisp
108 lines (97 loc) · 2.15 KB
/
binary_srch_with_sorting.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
(defun sort_array(A n)
(loop for i from 0 to (- (car (array-dimensions A)) 1)
do(
loop for j from i to (- (car (reverse(array-dimensions A))) 1 )
do
(
progn
(if (> (aref A i) (aref A j))
(
progn
(setf temp (aref A i))
(setf (aref A i) (aref A j))
(setf (aref A j) temp)
)
)
)
)
)
(return-from sort_array A)
)
(defun chk_sorted (A n)
(loop for i from 1 to (- n 1)
do(progn
(if (> (aref A (- i 1)) (aref A i) )
(
progn
(format t "Ohk Array was not sorted")
(terpri)
(format t "Sorting Array now...")
(terpri)
(return-from chk_sorted(sort_array A n))
)
)
)
)
(format t "Ohk Array was sorted")
(terpri)
(return-from chk_sorted A)
)
(defun binary_srch (A i j key)
(setq mid (floor (/ (+ i j) 2)))
(if (<= i j)
(cond
(
(= (aref A mid) key)
(
progn
(setf temp (- mid 1))
(setf indexes (list mid))
(loop while (= (aref A temp) key)
do(
progn
(setf indexes (cons temp indexes))
(setf temp (- temp 1))
)
)
(setf temp (+ mid 1))
(setf indexes (reverse(indexes)))
(loop while (= (aref A temp) key)
do(
progn
(setf indexes (cons temp indexes))
(setf temp (+ temp 1))
)
)
(setf indexes (reverse indexes)))
(format t "Found At indexes ~D" indexes)
)
(
(< (aref A mid) key)
(binary_srch A (+ mid 1) j key)
)
(
(> (aref A mid) key)
(binary_srch A i (- mid 1) key)
)
)
(format t "NOT FOUND IN MATRIX")
)
)
(format t "Enter N for to be Matrix to be searched")
(terpri)
(setf n1 (read))
(setf A (make-array n1))
(dotimes (i n1)
(
progn
(format t "Enter ~D th Element of Matrix" (+ i 1))
(terpri)
(setf (aref A i) (read))
)
)
(format t "Enter Number to be searched in matrix")
(terpri)
(setf key (read))
(setf A (chk_sorted A n1))
(binary_srch A 0 (- n1 1) key)