Application Development 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: 

binary seach in internal table

Former Member
0 Kudos
94

if not sort inernal table table , what happend in binary seach

3 REPLIES 3

Former Member
0 Kudos
63

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.

former_member223537
Active Contributor
0 Kudos
63

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

Former Member
0 Kudos
63

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.