‎2012 Jul 23 1:13 PM
Hi all,
i have a doubt when using BINARY SEARCH ...
As SAP says in the link "http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb373d358411d1829f0000e829fbfe/content.htm" :
- "The standard table must be sorted in ascending order by the specified search key."
So my questions are :
1. Binary search do not work if we sort the table in Descending order ?
2. In the sort case below: SORT itab BY matnr vtweg.
Can i directly Read the table with only the key matnr like below ? I mean, i don't need to sort again itab BY matnr (only)
READ TABLE itab
ASSIGNING <fs_itab>
WITH KEY matnr = lv_matnr
BINARY SEARCH.
My question is very basic but i think it is important to understand this concept ... hope to have an answer
Thanks in advance
Ainjaka
‎2012 Jul 23 1:21 PM
Hi,
1) No. Binary Search won't work in case of Descending Sort.
2) No. No need to sort again. This would work fine.
Thanks and Regards,
Chirag
‎2012 Jul 23 1:24 PM
Hello Ainjaka,
as you mentioned it before, the BINARY SEARCH Option works only within the specified parameters.
You cannot use a READ TABLE on a table where the sorting is not ascending by the specified key and you will get a SY-SUBRC = 8 if a violation of the table has been found.
In your case, you can only use "MATNR" as key if your table is sorted ascending because the binary search algorithm follows the key fields of the READ TABLE command. E.g. first search with MATNR - if there are no more fields, it takes the current result.
Regard: You can get multiple results when you use an incomplete key.
Better use table types with "SORTED" or "HASHED" Option.
Kind regards,
Hendrik
‎2012 Jul 23 1:27 PM
1. BINARY SEARCH doesn't work in descending.
2. Usually your SORT BY fields must be the same as your READ KEY fields. On your example it would be ok as you use only the first key fields. It would only dangerous if you use more READ KEY fields than what it is actually sorted.
‎2012 Jul 23 5:31 PM
Hi C Z,
Statement 2. is only true if you are using binary search. If you are not then it does not matter whether it is sorted or not.
Regards,
Shravan
‎2012 Jul 23 4:40 PM
Hi Ainjaka, although your doubt is already resolved above, I would like just to add an interesting link, hoping it helps you to clarify the concept around read table with binary search.
http://friendlyabaper.blogspot.mx/2006/10/pure-and-simple-truth-about-binary.html
Best Regards!
‎2012 Jul 23 5:34 PM
Hi
Forget ABAP - just search for what BINARY SEARCH is in programming and you would be able to understand the logic behind the answers to your question.
Regards,
Shravan