用java、C 做冒泡排序很常见,所以我这里想使用plsql写一个冒泡排序:
i 组数据:1、2、3、4、5、6
j 组数据:1、2、3、4、5、6
第一轮:i 组的 1 分别和 j 组的1、2、3、4、5、6 比较。
第二轮:i 组的 2 分别和 j 组的2、3、4、5、6 比较。
第三轮:i 组的 3 分别和 j 组的3、4、5、6 比较。
第四轮:i 组的 4 分别和 j 组的4、5、6 比较。
第五轮:i 组的 5 分别和 j 组的5、6 比较。
上代码:
declare
names varchar2(100) := '1,2,3,4,5,6';
names_adjusted varchar2(61);
comma_location number := 0;
prev_location number:= 0;
cur_id number;
v_count number;
--声明嵌套表集合
type type_array is table of integer;
v_array type_array := type_array();
type typ_dict is table of varchar2(100) index by varchar2(10); --关联数组
v_dict typ_dict;
l_row integer;
j_row integer;
i number;
begin
-- Test statements here
names_adjusted := names || ',';
v_count := 0;
i := 0;
--v_array.extend(20);
loop
comma_location := instr(names_adjusted,',',comma_location + 1);
exit when comma_location = 0;
cur_id := substr(names_adjusted,prev_location+1,comma_location - prev_location -1);
-- dbms_output.put_line(substr(names_adjusted,prev_location+1,comma_location - prev_location -1));
prev_location := comma_location;
v_count := v_count + 1;
--v_array(v_count) := cur_id;
v_dict(v_count) := cur_id;
end loop;
--为集合分配空间
-- v_array.extend(v_count);
l_row := v_dict.first;
for i in 1 .. v_count - 1 loop
j_row := v_dict(i);
dbms_output.put_line('The v_dict(' || i || ') is ' || v_dict(l_row) || ' i');
for j in i .. (v_count) loop
dbms_output.put_line('The v_dict(' || j || ') is ' || v_dict(j_row) || ' j');
j_row := v_dict.next(j_row);
end loop;
l_row := v_dict.next(l_row);
end loop;
end;
运行结果: