Error in PL/SQL bubble sorting program

i am writing a plsql bubble sorting program but it is giving a error “ORA-06532: Subscript outside of limit” kindly help me to resolve it as i am new to plsql

DECLARE

temp int;
type first is VARRAY(6) of integer;
arr first;
i integer;
j integer;
BEGIN
i:=0;
j:=1;
temp:=0;
arr:=first(3,1,4,2,5,9);
FOR i in 0…4 LOOP
FOR j in 1…5 LOOP
if arr(i)>arr(j) then
temp:=arr(i);
arr(i):=arr(j);
arr(j):=temp;
end if;
END LOOP;
END LOOP;
FOR i in 0…5 LOOP
dbms_output.put_line(arr(i));
END LOOP;

END

I modified your code and executed it. Pls see the executed code:

SQL> set serverout on size 1000000 timing on

SQL> DECLARE

temp int;

2 type first is VARRAY(6) of integer;

3 4 5 arr first;

6 k integer; --<<

7 BEGIN

8 temp:=0;

9 arr:=first(3,1,4,2,5,9);

10 k:=arr.count; --<<

11 FOR i in 1…k LOOP --<<

FOR j in 1…k LOOP --<<

12 13 if arr(i)>arr(j) then

14 temp:=arr(i);

15 arr(i):=arr(j);

16 arr(j):=temp;

17 end if;

18 END LOOP;

END LOOP;

19 20 FOR j in 1…k LOOP --<<

21 dbms_output.put_line(arr(j));

22 END LOOP;

23

24 END;

25 /

9

5

4

3

2

1

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.04

Hi jahanzaibniazi001,

You start indexes at 1. I modified your code.

DECLARE

temp INT;

type FIRST IS VARRAY (6) OF INTEGER;

arr FIRST;

i INTEGER;

j INTEGER;

BEGIN

arr := FIRST (3, 1, 4, 2, 5, 9) ;

FOR i IN 1…6

LOOP

FOR j IN i…6

LOOP

IF arr (i) > arr (j) THEN

temp := arr (i) ;

arr (i) := arr (j) ;

arr (j) := temp;

END IF;

END LOOP;

END LOOP;

FOR i IN 1…6

LOOP

dbms_output.put_line (arr (i)) ;

END LOOP;

END;