Home » SQL & PL/SQL » SQL & PL/SQL » Error in pl/sql Bubble sort code
Error in pl/sql Bubble sort code [message #630392] Thu, 25 December 2014 18:44 Go to next message
jahanzaibniazi001
Messages: 1
Registered: December 2014
Location: GJHGJQ
Junior Member
hello i am writing a bubble sort program in pl/sql but when i run it, it is giving error below is complete my code please help me...

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;

*BlackSwan added {code} tags. Please do so yourself in the future.

[Updated on: Thu, 25 December 2014 23:23] by Moderator

Report message to a moderator

Re: Error in pl/sql Bubble sort code [message #630396 is a reply to message #630392] Thu, 25 December 2014 23:21 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum.

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and read http://www.orafaq.com/forum/t/174502/


BTW, we don't do homework.
Re: Error in pl/sql Bubble sort code [message #630399 is a reply to message #630392] Fri, 26 December 2014 00:27 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
SQL> DECLARE
  2  
  3  temp int;
  4  type first is VARRAY(6) of integer;
  5  arr first;
  6  i integer;
  7  j integer;
  8  BEGIN
  9  i:=0;
 10  j:=1;
 11  temp:=0;
 12  arr:=first(3,1,4,2,5,9);
 13  FOR i in 0..4 LOOP
 14      FOR j in 1..5 LOOP
 15         if arr(i)>arr(j) then
 16            temp:=arr(i);
 17            arr(i):=arr(j);
 18            arr(j):=temp;
 19         end if;
 20      END LOOP;
 21  END LOOP;
 22  FOR i in 0..5 LOOP
 23      dbms_output.put_line(arr(i));
 24  END LOOP;
 25  
 26  END;
 27  /
DECLARE
*
ERROR at line 1:
ORA-06532: Subscript outside of limit
ORA-06512: at line 13


The error is clear: at one moment in the execution, at line 13, you use an index that does not exist in the array (below 1 or greater than 6).

[Updated on: Fri, 26 December 2014 00:28]

Report message to a moderator

Re: Error in pl/sql Bubble sort code [message #630400 is a reply to message #630399] Fri, 26 December 2014 00:30 Go to previous message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

PL/SQL Language Reference
Chapter 5 PL/SQL Collections and Records
Section Varrays (Variable-Size Arrays)
Previous Topic: Divide in Groups
Next Topic: ORA-01722: invalid number
Goto Forum:
  


Current Time: Mon Aug 24 02:20:04 CDT 2026