0

ユーザーが乱数のリストを入力し、バブル ソート アルゴリズムを使用してそれらをソートできるようにするストアド プロシージャを作成する際に助けが必要です。私はプログラミングとPL/SQLに非常に慣れていません。どんな助けでも大歓迎です。

以下は、これまでに持っているコード行です。

CREATE OR REPLACE PROCEDURE test_BubbleSort (i_number IN number) AS

type l_array_type IS TABLE OF NUMBER(10);

l_temp  NUMBER;

l_array l_array_type := l_array_type();

BEGIN

  --Loop through numbers and re-arrange their order using bubble sort---

  FOR i in 1 .. l_array.Count - 1 LOOP

    FOR j IN 2 .. l_array.Count LOOP
      IF l_array(j) > l_array(j - 1) THEN
        l_temp := l_array(j - 1);
        l_array(j - 1) := l_array(j);
        l_array(j) := l_temp;
      END IF;
    END LOOP;
  END LOOP;

  --Print the newly sorted numbers user inputs 

  FOR i in REVERSE 1 .. l_array.COUNT LOOP

    dbms_output.put_line('The new sorted numbers are: ' || l_array(i));
  END LOOP;

END;
4

2 に答える 2

1

これは本当にあなたが望むものではないと思いますが、実際に自分で乱数を生成したいだけで、ユーザーがリストの長さを提供したいだけなら ( as i_number)、ループしてそれを行うことができます:

...
BEGIN

  --Generate some random numbers
  for i in 1..i_number loop
    l_array.extend;
    l_array(i) := dbms_random.value(1, 100);
  end loop;

  --Loop through numbers and re-arrange their order using bubble sort---

  FOR i in 1 .. l_array.Count - 1 LOOP
  ...

パラメータ値 5 で呼び出された場合、次のi_numberようになります。

The new sorted numbers are: 10
The new sorted numbers are: 55
The new sorted numbers are: 60
The new sorted numbers are: 74
The new sorted numbers are: 87

呼び出しのパラメーターは、dbms_random.value()生成される「乱数」の範囲を制限しています。

于 2013-04-29T15:49:54.693 に答える