2006 Aug 08 5:22 AM
if not sort inernal table table , what happend in binary seach
2006 Aug 08 5:24 AM
The binary search algorithm helps faster search of a value in an internal table. It is advisable to sort the internal table before doing a binary search. Binary search repeatedly divides the search interval in half. If the value to be searched is less than the item in the middle of the interval, the search is narrowed to the lower half, otherwise the search is narrowed to the upper half.
IF the int table is sorted by a key, then when BINARY SEARCH is added to a WITH KEY statement... the "read" to the int table is performed at a binary level for maximum performance.
if N is the number of entries in your itab, linear search is done in O(N) and binary search is done in O(logN).
use it if your itab is big, just don't forget to sort it if it's a standard table.
2006 Aug 08 5:26 AM
Hi,
If the table is not sorted and you use Binary search then it will search the record start from 1st record. If 20 records are in internal table, then it will start from 1st record.
Instead if you sort the table, it will start from the middle. i.e. if 20 records are in internal table then it will start from 11th record upwards or downwards depending on the value. So the time required would be half the time of normal.
Best regards,
Prashant
2006 Aug 08 5:27 AM
Hi,
You must SORT internal table.
Excample.
If you dont SORT properly defnetly BINARY SEARCH will failed.
Make sure to takecare the order of the fields on which you are sorting.
SORT itab by bukrs belnr gjahr.
loop at it_itab.
clear itab.
read table itab with key bukrs = it_itab-bukrs
belnr = it_itab-belnr
gjahr = it_itab-gjahr.
endloop.
Thanks.
If this helps you reward with points.