‎2009 May 15 8:07 AM
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!!
‎2009 May 15 8:09 AM
Helo,
Table must be in sorted order it might be in Asc OR Desc.
Regards
‎2009 May 15 8:09 AM
hI
When using Binary search in read statement, sort internal table in ascending order of the keys used in read statement
‎2009 May 15 8:11 AM
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
‎2009 May 15 8:12 AM
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
‎2009 May 15 8:19 AM
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!!
‎2009 May 15 8:16 AM
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
‎2009 May 15 8:31 AM
HI Thomas you are write and this is my question, why ascending order is necessary, binary search algo does not put any such restriction!
‎2009 May 15 8:45 AM
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
‎2009 May 15 8:46 AM
‎2009 May 15 8:53 AM
‎2010 Jul 20 11:28 AM
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.