oracle heap_sort

CREATE OR REPLACE PACKAGE heap_sort_pkg

AS

  TYPE num_arr_tt IS TABLE OF PLS_INTEGER INDEX BY PLS_INTEGER;

  PROCEDURE add_nums(num_arr_inst IN OUT num_arr_tt, dvalue INT);

  PROCEDURE del_nums(num_arr_inst IN OUT num_arr_tt);

  FUNCTION  get_maxval(num_arr_int IN  OUT num_arr_tt) RETURN INT;

END heap_sort_pkg;



CREATE OR REPLACE PACKAGE BODY heap_sort_pkg

AS

  PROCEDURE add_nums(num_arr_inst IN OUT num_arr_tt, dvalue INT)

  AS

     dest_loc INT := num_arr_inst.count + 1;

     tmp_loc  INT := trunc(dest_loc / 2);

  BEGIN

     WHILE(tmp_loc > 0 AND dvalue > num_arr_inst(tmp_loc)) LOOP

         num_arr_inst(dest_loc) := num_arr_inst(tmp_loc);

         dest_loc := tmp_loc;

         tmp_loc := trunc(dest_loc / 2);

     END LOOP;

     num_arr_inst(dest_loc) := dvalue;

  END add_nums;


  PROCEDURE del_nums(num_arr_inst IN OUT num_arr_tt)

  AS

     lvalue INT;

     rvalue INT;

     umark  INT := 1;

     tmark  INT := umark;

     tvalue INT := num_arr_inst(num_arr_inst.count);

  BEGIN

     IF(num_arr_inst.count <= 1) THEN

        num_arr_inst.delete(1);

        dbms_output.put_line(‘只有一个值或无值‘);

        RETURN;

     END IF;

     num_arr_inst.delete(num_arr_inst.count);

     lvalue := umark * 2;

     rvalue := umark * 2 + 1;

     WHILE(umark <= num_arr_inst.count/2) LOOP

         IF(num_arr_inst.exists(lvalue) AND num_arr_inst.exists(rvalue) AND num_arr_inst(lvalue) < num_arr_inst(rvalue)) THEN

             tmark := rvalue;

         ELSE

             tmark := lvalue;

         END IF;

         IF(tvalue < num_arr_inst(tmark)) THEN

             num_arr_inst(umark) := num_arr_inst(tmark);

         ELSE

             EXIT;

         END IF;

         umark := tmark;

         lvalue := umark * 2;

         rvalue := umark * 2 + 1;

     END LOOP;

     num_arr_inst(umark) := tvalue;

  END del_nums;


  FUNCTION get_maxval(num_arr_int IN OUT num_arr_tt) RETURN INT

  AS

      max_value INT := -1;

  BEGIN

      IF(num_arr_int.exists(1)) THEN

        max_value := num_arr_int(1);

        del_nums(num_arr_int);

      END IF;

      RETURN max_value;

  END get_maxval;

END heap_sort_pkg;


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。