Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Binary Search

Former Member
0 Likes
676

Hi SDN Experts

Is it must to use a sort statement before we use a binary search in a read statement. because in some cases its failing as SY-SUBRC = 4 if my internal table is not sorted.

Please confirm me

Regards

Pratyusha

1 ACCEPTED SOLUTION
Read only

gopi_narendra
Active Contributor
0 Likes
642

Its a must to SORT before you use the binary search on an internal table

sort it_final by BUKRS ANLN1 ANLN2.
  LOOP AT it_final INTO is_final.
      IF flag = 'X'.
        READ TABLE it_output INTO is_output WITH KEY
                                  bukrs = is_final-bukrs
                                  anln1 = is_final-anln1
                                  anln2 = is_final-anln2
                                  BINARY SEARCH.
        IF sy-subrc = 0.
          is_output-c_icon  = c_green.
          is_output-grufl_n = is_final-grufl.
          is_output-message = it_return-message.
* Modify the output internal table with the changed new values
          MODIFY it_output FROM is_output
                           TRANSPORTING c_icon grufl_n message
                           WHERE bukrs = is_output-bukrs
                             AND anln1 = is_output-anln1
                             AND anln2 = is_output-anln2.
        ENDIF.
Endloop.

Regards

Gopi

5 REPLIES 5
Read only

Former Member
0 Likes
642

Hi,

Its mandatory that the internal table should be sorted before binary search.

e.g.

ITAB

f1 f2

1 a

2 d

5 f

3 g

Suppose we want to read Itab for Value f1 = 3,

It will give me sy-subrc = 4.

But if ITAB is sorted on f1

then

ITAB

f1 f2

1 a

2 d

3 g

5 f

It will give me sy-subrc = 0.

Regards,

Ranjit Thakur.

<b>Please Mark The Helpful Answer.</b>

Read only

gopi_narendra
Active Contributor
0 Likes
643

Its a must to SORT before you use the binary search on an internal table

sort it_final by BUKRS ANLN1 ANLN2.
  LOOP AT it_final INTO is_final.
      IF flag = 'X'.
        READ TABLE it_output INTO is_output WITH KEY
                                  bukrs = is_final-bukrs
                                  anln1 = is_final-anln1
                                  anln2 = is_final-anln2
                                  BINARY SEARCH.
        IF sy-subrc = 0.
          is_output-c_icon  = c_green.
          is_output-grufl_n = is_final-grufl.
          is_output-message = it_return-message.
* Modify the output internal table with the changed new values
          MODIFY it_output FROM is_output
                           TRANSPORTING c_icon grufl_n message
                           WHERE bukrs = is_output-bukrs
                             AND anln1 = is_output-anln1
                             AND anln2 = is_output-anln2.
        ENDIF.
Endloop.

Regards

Gopi

Read only

seshatalpasai_madala
Product and Topic Expert
Product and Topic Expert
0 Likes
642

HI,

If the table is of type STANDARD INTERNAL TABLE then you need to SORT IT,

For sorted table you can use it normally.

You cannot use BINARY SEARCH for HASHED TABLES.

Regards,

Sesh

Read only

Former Member
0 Likes
642

Hi,

As Internal table breaks into two parts it should be sorted out.

And this happens to reduce search area again and again.

Reward if useful!

Read only

Former Member
0 Likes
642

Hi,

Yes, it is mandatory to sort the table before doing a binary search.

The principle of binary search only work in case of sorted tables.

A binary search algorithm is a technique for finding a particular value in a sorted list. A binary search finds the median element in a list, compares its value to the one you are searching for, and determines if it’s greater than, less than, or equal to the one you want. A guess that turns out to be too high becomes the new top of the list, and one too low the new bottom of the list. The binary search's next guess is halfway between the new list's top and bottom. Pursuing this strategy iteratively, it narrows the search by a factor 2 each time, and finds your value. A binary search is an example of a divide and conquer algorithm.

More on binary search.

The idea is to eliminate half of the search space with each comparison.

First, the middle element of the sequence is compared to the value we are searching for. If this element matches the value we are searching for, we are done. If, however, the middle element is “less than” the value we are chosen for (as specified by the relation used to specify a total order over the set of elements), then we know that, if the value exists in the sequence, it must exist somewhere after the middle element. Therefore we can eliminate the first half of the sequence from our search and simply repeat the search in the exact same manner on the remaining half of the sequence. If, however, the value we are searching for comes before the middle element, then we repeat the search on the first half of the sequence.

Hope this helps.

Regards,

Kate