pl/sql 氣泡排序一例

dengxm發表於2011-09-20
昨天寫了個排序演算法,記錄一下
-----------------------------------------------------------------------------------------
DECLARE
   TYPE mylist_type IS TABLE OF NUMBER
      INDEX BY PLS_INTEGER;
   mylist   mylist_type;
   n_temp   NUMBER;
BEGIN
   mylist (0) := 3;
   mylist (1) := 2;
   mylist (2) := 1;
   mylist (3) := 4;
   mylist (4) := 0;
   FOR i IN REVERSE 0 .. mylist.COUNT - 1
   LOOP
      FOR j  IN 1 .. i
      LOOP
         IF mylist (j-1) <= mylist (j)
         THEN
            n_temp := mylist (j-1);
            mylist (j-1) := mylist (j);
            mylist (j) := n_temp;
         END IF;
      END LOOP;
      DBMS_OUTPUT.put_line ('mylist (' || i || ')' || '=' || mylist (i));
   END LOOP;
END;
/

mylist (4)=0
mylist (3)=1
mylist (2)=2
mylist (1)=3
mylist (0)=4

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/13726712/viewspace-707908/,如需轉載,請註明出處,否則將追究法律責任。

相關文章