-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbinarysearch.sql
More file actions
41 lines (37 loc) · 778 Bytes
/
binarysearch.sql
File metadata and controls
41 lines (37 loc) · 778 Bytes
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
DECLARE
TYPE intarray IS VARRAY(10) OF INTEGER;
a intarray;
first INTEGER;
mid INTEGER;
last INTEGER;
flag BOOLEAN;
key INTEGER;
BEGIN
key := 10;
first :=0;
last :=10;
flag := FALSE;
a := intarray(1,2,3,4,5,6,7,8,9,10);
WHILE(first <= last) LOOP
BEGIN
mid := (first + last) / 2;
IF(a(mid) = key)
THEN
dbms_output.put('number found @ ');
dbms_output.put_line(mid);
flag := TRUE;
EXIT;
END IF;
IF(a(mid) > key)
THEN
last := mid -1;
ELSE
first := mid + 1;
END IF;
END;
END LOOP;
IF(NOT flag)
THEN
dbms_output.put_line('number not found');
END IF;
END;