简体   繁体   English

PL / SQL Bubble排序程序错误

[英]Error in PL/SQL Bubble sorting program

I am writing a PL/SQL bubble sorting program but it is giving a: 我正在编写一个PL / SQL气泡排序程序,但是它给出了:

Error "ORA-06532: Subscript outside of limit"

error. 错误。 Kindly help me to resolve it as I am new to PL/SQL, the code is: 请帮我解决它,因为我是PL / SQL的新手,代码是:

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

PL/SQL arrays are one-based rather than zero-based. PL / SQL数组是基于1的,而不是基于零的。

Hence the indexes you need to use are 1..6 rather than 0..5. 因此,您需要使用的索引是1..6而不是0..5。

That should solve your immediate problem. 那应该解决您的直接问题。 Whether your algorithm will work may be another matter, it looks like a hybrid between bubble and selection sorts - the former tends to only compare adjacent elements. 算法是否有效可能是另一回事,它看起来像冒泡和选择排序之间的混合体-前者往往只比较相邻元素。

But fixing the array index out of bounds issue is your first step. 但是,解决数组索引问题是您的第一步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM