繁体   English   中英

pl / sql冒泡排序

[英]pl/sql bubble sort

好吧,我在为此打败自己。 我需要在存储在表中的人姓中加载一个数组。 然后对姓氏进行排序并按字母顺序打印出来。 必须使用冒泡排序算法完成此操作。

这是我到目前为止所拥有的

CREATE OR REPLACE PROCEDURE TEAM_TABLE_SORT AS
  TYPE player_Name_type IS TABLE OF databasename.team.player%type
  INDEX BY PLS_INTEGER ;
  player_name player_Name_type;
  i integer := 1;
  temp integer;

BEGIN

  FOR player_names IN (SELECT * FROM marshall.team )
  LOOP
    player_name(i) := player_names.player;
    DBMS_OUTPUT.PUT_LINE(i|| ' - ' ||chr(9) || player_name(i) ) ;
    i := i + 1 ;
  END LOOP

这一切真的是打印出名字。 我无法理解它。 我不是在尝试这件事

TYPE player_Name_type IS TABLE OF  %type INDEX BY varchar2(20) ;
aux player_Name_type;
i integer := 1;
v_current is table of aux
swapped BOOLEAN := TRUE;

BEGIN

  FOR aux IN (SELECT * FROM )
  LOOP
    DBMS_OUTPUT.PUT_LINE(i|| ' - ' ||chr(9) || aux.player);
    i := i + 1 ;
  END LOOP;

  v_current := aux.first;
  WHILE(swapped)
  LOOP
    swapped := FALSE;

    FOR I IN 1..(aux.count-2) LOOP
      IF aux(i) > aux(I+1) THEN
         v_current := aux(i+1);
         aux(I+1) := aux(i);
         aux(i) :=  v_current;
      END IF;
      swapped := TRUE;

    END LOOP;

  END LOOP;

FOR aux IN (SELECT * FROM    LOOP

  DBMS_OUTPUT.PUT_LINE(i|| ' - ' ||chr(9) ||aux.player);
  i := i + 1 ;
END LOOP;

这应该是你正在寻找的。 请注意,最好从表中输入变量/集合,就像在示例中一样。 我只使用通用版本,因为我没有你的表可以使用。 如果您不明白这是如何工作的,请随时问。 我猜这是家庭作业(还有谁会在Oracle中冒泡),所以分配的重点是让你理解它,而不仅仅是为了正确。 :)

DECLARE
  coll    DBMS_SQL.VARCHAR2A;
  swapped BOOLEAN;
  tmp     VARCHAR2(10);
BEGIN
  /*
    Generate 10 random strings and collect them into our collection
    Note: you would replace this with your query on marshall.team
  */
  select dbms_random.string('l',10) rand_string
  BULK COLLECT INTO coll
  from dual
  connect by level <= 10;


  /*
    At this point, all of the rows we need are in our collection
    so there is no need to go back to the table anymore.  Now onto the...

    Bubble sort.. walk through the collection swapping elements until
    we make a pass where no swapping takes place
  */
  LOOP

   swapped := false;

   FOR i IN 2 .. coll.LAST
   LOOP

     IF coll(i-1) > coll(i)
     THEN
       -- swap records
       tmp := coll(i);
       coll(i) := coll(i-1);
       coll(i-1) := tmp;

       /*
         Mark that swap has taken place.  note we mark as true only inside
         the if block, meaning a swap really did take place
       */ 
       swapped := true;

      END IF;

   END LOOP;

   -- If we passed through table without swapping we are done, so exit
   EXIT WHEN NOT swapped;

  END LOOP; 

  /*
    Now print out records to make sure they are in order.  Again notice
    how we are just referencing the (now sorted) collection and not going
    back to the table again
  */
  FOR i in coll.FIRST .. coll.LAST
  LOOP

    dbms_output.put_line(coll(i));

  END LOOP;

END;
/

您通常希望在源查询中使用ORDER BY。

您也可以使用VARCHAR2索引表进行排序。

DECLARE
  CURSOR c_1 is
     SELECT table_name, num_rows FROM user_tables order by num_rows;
  TYPE typ_tab IS TABLE OF c_1%rowtype INDEX BY user_tables.table_name%type;
  t_tab typ_tab;
  v_str user_tables.table_name%type;
BEGIN
  FOR c_rec IN c_1 LOOP
    t_tab(c_rec.table_name) := c_rec;
  END LOOP;
  v_str := t_tab.first;
  WHILE v_str IS NOT NULL LOOP
    dbms_output.put_line(t_tab(v_str).table_name||':'||t_tab(v_str).num_rows);
    v_str := t_tab.next(v_str);
  END LOOP;
END;
/

您发布的第二个代码块看起来像是冒泡排序算法的有效实现。 它看起来不起作用的原因是因为最后一个循环。 您不是打印出已排序的数组,而是使用表中随机排序的数据重新填充它。

所以,只需更改最后一个循环:

FOR i IN 1..aux.count()
LOOP    
    DBMS_OUTPUT.PUT_LINE(i|| ' - ' ||chr(9) ||aux(i).player);
END LOOP; 

您还可以在此网站上使用PL / SQL中的冒泡排序示例:

http://www.oratechinfo.co.uk/oo.html#bubble_sort

暂无
暂无

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

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