pl/sql 求二分法

create or replace procedure pr_half_find(in_num in number,in_arr in varchar2)
is

type arr_num is table of number index by binary_integer;
my_arr_num arr_num;
n_arr_index number(10):=0;

n_upper_bound number(10);
n_lower_bound number(10);
n_cur_index number(10);
begin

   for i in (select my_date from (
                    select regexp_substr(in_arr,'[^,]+',1,rownum) my_date from dual
                    connect by rownum<= length(in_arr) - length(replace(in_arr,',',''))+1
                    )
             order by to_number(my_date))
   loop
      n_arr_index := n_arr_index+1;
      my_arr_num(n_arr_index) := i.my_date;
      dbms_output.put_line(n_arr_index||'   '||my_arr_num(n_arr_index));

   end loop;
  
   n_upper_bound := 1;
   n_lower_bound := n_arr_index;
  
   if in_arr  is not null then
       while true
       loop
          n_cur_index := floor((n_upper_bound + n_lower_bound)/2);
          if my_arr_num(n_cur_index) = in_num then
             dbms_output.put_line('在第'||n_cur_index||'位');
             exit;
          end if;
         
          if n_upper_bound>=n_lower_bound then
             exit;
          end if;
          if my_arr_num(n_cur_index) > in_num then
             n_lower_bound := n_cur_index;
          else
             n_upper_bound := n_cur_index+1;
          end if;
         
       end loop;  
   end if;

end;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值