pl/sql 冒泡排序

发布于 2024-09-30 09:52:12 字数 1305 浏览 2 评论 0原文

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

这就是我到目前为止所做的

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;

okay, I am beating myself up over this. I am need to load a array in people last name stored in a table. Then sort the last names and print them out in alphabetical order. This must be done using the bubble sort algorithm.

here is what I have so far

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

All this really does is print out the names. I cannot get it to sort. I am not try thing this

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我不是你的备胎 2024-10-07 09:52:12

这应该就是您正在寻找的。请注意,最好像示例中那样从表中键入变量/集合。我只是使用通用版本,因为我没有您的表可供使用。如果您不明白这是如何工作的,请随时询问。我猜这是家庭作业(还有谁会在 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;
/

This should be what you are looking for. Note that it is better to type the variables/collections off of the tables like you have in your example. I just used generic versions since I don't have your tables to work with. If you don't understand how this is working, feel free to ask. I am guessing this is homework (who else would bubble sort in Oracle), so the point of the assignment is for you to understand it, not just to get it right. :)

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;
/
半﹌身腐败 2024-10-07 09:52:12

您通常希望在源查询中使用 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;
/

You generally want to use an ORDER BY in the source query.

You can use a VARCHAR2 index-by table to do sorting as well though.

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;
/
一袭白衣梦中忆 2024-10-07 09:52:12

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

因此,只需更改最后的循环:

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

The second chunk of code you have posted looks like a valid implementation of the bubble sort algorithm. The reason it appears not to work is because of that final loop. Instead of printing out the sorted array you are repopulating it with randomly-ordered data from your table.

So, just change the final loop:

FOR i IN 1..aux.count()
LOOP    
    DBMS_OUTPUT.PUT_LINE(i|| ' - ' ||chr(9) ||aux(i).player);
END LOOP; 
若相惜即相离 2024-10-07 09:52:12

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

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

You can also use the example of the Bubble Sort in PL / SQL on this website:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文