ABAP 實現選擇排序

2021-02-24 14:26:38 浏览数 (1)

代码语言:javascript复制
*selection sort
PARAMETERS: lines    LIKE sy-index,
            no_range LIKE sy-index.
TYPES: BEGIN OF number,
         no TYPE i,
       END OF number.
DATA: s_number TYPE number.

DATA: t_number TYPE TABLE OF number WITH HEADER LINE.
DATA: t_number1 TYPE TABLE OF number WITH HEADER LINE.
DATA: cl_random TYPE REF TO cl_random_number.

DATA: m        TYPE i,
      num      TYPE i,
      l_index1 TYPE i.

CREATE OBJECT cl_random.
CALL METHOD cl_random->if_random_number~init.

START-OF-SELECTION.
  "添加隨機數賦給內表
  DO lines TIMES.
    CALL METHOD cl_random->if_random_number~get_random_int
      EXPORTING
        i_limit  = no_range "随机数范围
      RECEIVING
        r_random = m.
    t_number = VALUE #( no = m ).
    APPEND t_number.
  ENDDO.
*排序
  DO lines TIMES.
    READ TABLE t_number INDEX 1.
    num = t_number-no.
    LOOP AT t_number.
      IF t_number-no < num.
        num = t_number-no.
        CLEAR: l_index1.
      ELSEif t_number-no = num.
        clear: l_index1.
        num = t_number-no.
        l_index1 = sy-tabix.
      ENDIF.
    ENDLOOP.
    t_number1-no =  num .
    APPEND t_number1.
    IF l_index1 IS NOT INITIAL.
      DELETE t_number INDEX l_index1.
      CLEAR: l_index1.
    ELSE.
      DELETE t_number WHERE no = num .
      CLEAR: num.
    ENDIF.
  ENDDO.
cl_demo_output=>display( t_number1[] ).

我的博客即将同步至腾讯云 社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=hrhanlh0q126

0 人点赞