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

Read table and binary search

Former Member
0 Likes
3,487

Hi all,

I have a simple query regarding read int_tab with binary search.

Why reading internal table with binary search fails if it is sorted in descending order table must be sorted in ascending order?

I check fo the algorithm of binary search, it does not talk about sort order. As far as my understanding goes binary search only require sorted table but while reading table in SAP it has to be sorted in ascending order!!

11 REPLIES 11
Read only

shishupalreddy
Active Contributor
0 Likes
1,879

Helo,

Table must be in sorted order it might be in Asc OR Desc.

Regards

Read only

Former Member
0 Likes
1,879

hI

When using Binary search in read statement, sort internal table in ascending order of the keys used in read statement

Read only

Former Member
0 Likes
1,879

hi

in binary search it split the table and check the extreme values.

so upper most value is the low value and the lower most is the high value

so u need to short the table in decending order

Read only

Former Member
0 Likes
1,879

hi

before reading itab using binary search...with key conditions..

you must sort it...ascen/desc with order of the keys given in the read statment ...

please send the code..now.. if you face the problem again...

safel

Read only

0 Likes
1,879

If sorted in descending order then the read with binary search will fail .

try reading any table sorted in descending order with binary search, then the read will fail. Table must be sorted in ascending order and this is the issue , why it do not work if table is sorted in descending order.

Try writing a simple query and check!!

Read only

ThomasZloch
Active Contributor
0 Likes
1,879

No surprise:

> The table must be sorted by the specified search key in ascending order. Otherwise the wrong row will be found.

Source: http://help.sap.com/abapdocu/en/ABAPREAD_TABLE_FREE.htm#!ABAP_ONE_ADD@1@

Thomas

Read only

0 Likes
1,879

HI Thomas you are write and this is my question, why ascending order is necessary, binary search algo does not put any such restriction!

Read only

0 Likes
1,879

Well, the ABAP binary search algorithm has this restriction, and as for the "why", I have asked to have your question moved to the "ABAP performance" forum, I'm sure somebody will have a good answer there, as I could only make assumptions.

Thomas

Read only

0 Likes
1,879

Thank you thomas

Read only

Former Member
0 Likes
1,879

Posting to performance forum

Read only

Former Member
0 Likes
1,879

By default binary search assumes that the sort order is ASCENDING.

If you sort the list in descending and then try to binary search your quires will fail. Look at an example:

-


Let the descending order internal table as:

Field1 Field2

Sam 50000

John 34786

Boob 54321

Alice 12345

-


When you do binary search with key = 'Sam' then it will directly go to 2nd and 3rd records for comparision. The binary search algorithm compares 'Sam' with 'John' and it concludes that 'Sam' is greater than 'John' and it will continue to look downward into the internal table. And when it reaches the end of the internal table then the value of SY-TABIX = 5 and SY-SUBRC = 8 (Key is greater than the all).

And if you do binary search with key = 'Alice' then the binary search algorithm compares 'Alice' with 'John' and it concludes that 'Alice' is lower than 'John' and it will continue to look upward into the internal table. And when it reaches above the first record in internal table then the value of SY-TABIX = 1 and SY-SUBRC = 4 (points to the next largest entry).

The only correct result you will get is when you execute statement with key='John' (In this particular case) . SY-TABIX = 2 and SY-SUBRC = 0. I think you got this binary search algorithm.