Bubble Sort in ABAP

Preface – This post is part of the ABAP Programs series.

Bubble Sort in ABAP is an important topic in terms of interview question. In general, ABAP provides keyword “SORT” for sorting purpose and that is widely used everywhere. In this article, we will learn: What is a bubble sorting and how to implement it in our ABAP programs.

What is a Bubble Sorting?

A Bubble Sorting is a way to sort given array of numbers. The sorting mechanism involves a procedure in which every value of array (in case of ABAP, Internal Table) is compared with the adjacent value and swapped in case the upper value is greater than lower value as shown in image here:

bubble sort

The program to perform Bubble sort in ABAP is shown below:

DATA : ITAB_data       TYPE TABLE OF int4, "Internal Table to accept multiple data
       ITAB            TYPE TABLE OF string,
       wa_current_data type int4, " Work Area for this Internal Table
       wa_bubble_data  TYPE int4,
       lv_lines        TYPE int1,
       lv_data1        TYPE int2,
       lv_data2        TYPE int2,
       lv_current_line TYPE int1.

SELECTION-SCREEN BEGIN OF SCREEN 0100 . "Where we create a Screen 0100
SELECT-OPTIONS: s_data FOR wa_current_data. "Seclect-Option for multiple Input
DATA :   wa_sdata LIKE LINE OF s_data.

START-OF-SELECTION. " Where we show output
  CALL SELECTION-SCREEN '0100' STARTING AT 10 10. "Where we call the Screen 0100 we created earlier.
  LOOP AT s_data INTO wa_sdata.
    APPEND wa_sdata-low TO ITAB_data.

  DESCRIBE TABLE ITAB_data LINES lv_lines." Here we get total number of record
  WHILE sy-index < lv_lines. " Loop the whole record till all data are compared
    READ TABLE ITAB_data INTO wa_bubble_data INDEX 1. "Read 1st data into a field
    LOOP AT ITAB_data INTO  wa_current_data   FROM 2 TO lv_lines - sy-index + 1. " Read data from 2nd record onwards
      lv_current_line = sy-tabix. " Gives current loop number
      IF ( wa_bubble_data > wa_current_data ). " If record no.2 > record no.1 => SWAP them
        MODIFY ITAB_data FROM wa_bubble_data INDEX lv_current_line.
        MODIFY ITAB_data FROM wa_current_data INDEX lv_current_line - 1.
        wa_bubble_data = wa_current_data. " If not, 2nd record will become first and 3rd record will become 2nd record

  LOOP AT ITAB_data INTO wa_current_data.
    WRITE : wa_current_data.


In the program above, initially we have defined data types: Two internal tables [one to store original and another sorted], two work area [To be used by these internal tables], two variables [to be used for swaping], one variable [to save current line/index].

We have then created a selection screen to take input and save it in an internal table. Then using DESCRIBE, we will get the total number of records in that internal table. Then we use two loops [one inside another] and compare all the data and keep swaping them accordingly. And in last print the records as output that is sorted.


  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.


2 responses to “Bubble Sort in ABAP”

  1. Hi, thanks for sharing your solution.

    I don’t get the point of line 23. Why is it every time it goes on at index 1?

    Thx in advance.

    Kind regards

  2. Wonderful program!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.