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

ABAP - Sort in loop or read w/o binary search

Former Member
4,697

Hi Expert,

I need your advice on using a SORT statement inside a loop or using read without Binary search inside loop.

i.e. Loop IT_TAB. SORT IT_TAB2 (IT_TAB2 table is populated with data inside same loop) . Read IT_TAB2 using Binary search. Endloop,

OR Loop IT_TAB. Read IT_TAB2 without Binary search. Endloop.

Which one is better?

Regards,

Raj

1 ACCEPTED SOLUTION
Read only

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
3,321

Do not use SORT + BINARY SEARCH method!

There are much better options for fast internal table access / processing. Try SORTED or HASHED tables or at least use secondary table keys (SORTED or HASH).

-- Tomas --

Hi Expert,

I need your advice on using a SORT statement inside a loop or using read without Binary search inside loop.

i.e. Loop IT_TAB. SORT IT_TAB2 (IT_TAB2 table is populated with data inside same loop) . Read IT_TAB2 using Binary search. Endloop,

OR Loop IT_TAB. Read IT_TAB2 without Binary search. Endloop.

Which one is better?

Regards,

Raj

7 REPLIES 7
Read only

harald_lesche-holzbecher
Product and Topic Expert
Product and Topic Expert
0 Likes
3,321

You say "IT_TAB2 table is populated with data inside same loop"

So I suggest 3rd approach:

- sort IT_TAB2 prior to Loop IT_TAB (in case there are entries already)

- within Loop IT_TAB add new entries to IT_TAB2 so it is sorted

- you may now Read IT_TAB2 with Binary search without necessity to sort again

Read only

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
3,322

Do not use SORT + BINARY SEARCH method!

There are much better options for fast internal table access / processing. Try SORTED or HASHED tables or at least use secondary table keys (SORTED or HASH).

-- Tomas --
Read only

Sandra_Rossi
Active Contributor
0 Likes
3,321

If I understand your question well, IT_TAB2 is populated inside the loop in both cases, and you ask which of solution 1 (SORT + READ binary search) or solution 2 (READ without binary search) is the best.

First of all, the question is the same if there's no loop at all.

Let's calculate the "order":

Solution 1: SORT will read ALL the lines of a table (N)

Solution 2: READ without binary search will statistically tend to read half the lines if values are distributed equally and the searched value is random (N/2).

Answer: solution 2.

PS: I didn't even count the additional time needed for solution 1, to rebuild the index of lines, and to do a binary search.

Read only

Sandra_Rossi
Active Contributor
0 Likes
3,321

Is IT_TAB2 cleared & populated in each loop, or some lines are just added or removed?

Read only

s1252
Product and Topic Expert
Product and Topic Expert
0 Likes
3,321

If IT_TAB2 is type of a sorted table, we don’t have to sort it again and we can use binary search.

Read only

matt
Active Contributor
0 Likes
3,321

Don't use BINARY SEARCH. Use a sorted or hashed table. See Tomas Buryanek's answer below.

Binary search should not appear in modern programs. It isn't needed.

Read only

matt
Active Contributor
3,321

s1252 If it's a SORTED table, then you don't need the addition BINARY SEARCH. It will be done automatically (assuming you're using the table key).