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

binary seacrh

Former Member
0 Likes
388

hi,

What is read with binary search?

2 REPLIES 2
Read only

Former Member
0 Likes
352

If you read entries from standard tables using a key other than the default key, you can use a binary search instead of the normal linear search. To do this, include the addition BINARY SEARCH in the corresponding READ statements.

READ TABLE <itab> WITH KEY <k1> = <f1>... <kn> = <fn> BINARY SEARCH.

The standard table must be sorted in ascending order by the specified search key. The BINARY SEARCH addition means that you can access an entry in a standard table by its key as quickly as you would be able to in a sorted table.

BINARY SEARCH approaches the middle entry in the table, decides if it matches, or lexically greater than or equal to the key being searched. It accordingly skips either the top or bottom part of the internal table and searches the other half. It repeats this process till it finds the row.

Using BINARY SEARCH with READ is the most efficient way to read standard internal tables which are sorted by the key used to search them

BINARY SEARCH addition when reading a sorted table is not required, as it happens by default. It makes a good difference in performance if you are reading a large standard internal table without sorting and reading by BINARY SEARCH

Best regards,

Vishnu T

Read only

Former Member
0 Likes
352

Hi

Let me explain with an example. Lets assume there is an Internal table t_employee with records of 10 employees with Employee numbers 1 to 10 and sorted by Employee number(EMPNR) in the following fashion.

EMPNR

1

2

3

4

5

6

7

8

9

10.

Now lets assume i have to search for the record of employee 7.

READ TABLE t_employees WITH KEY empnr = '7' BINARY SEARCH.

The system will first hit the mid record of the table i.e '5'. Now it will check if 7 > 5. YES. So it will take the second half of the table viz 6 to 10 and hit the mid record i.e '8'. Now it will check if 7<8. YES. Now it will take the first half i.e 6 to 8 and hit at '7'. So 7 = 7. Thus this record will be populated into the header line. The total number of iterations is 3. If we did not user BINARY SEARCh, total number of iterations is 7.

I hope this helped.