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

hello, everybody, regarding a BINARY SEARCH.

Former Member
0 Likes
648

Hello experts:

Thank you in advance for all your replies.

READ TABLE VALUE WITH KEY KONPKEY BINARY SEARCH.

how does this statement work? would you please explain it with an example? cheers

couldn't thank you more.

Best regards.

Frank

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
618

Hello,

Make sure that the internal table that is used for READ statement should be sorted(either manually using SORT statement or using a SORTED internal table declaration)

Suppose there are values in internal table itab like

1

2

3

4

5

6

7

Here When u use READ TABLE itab WITH KEY (fieldname) = ' value ' BINARY SERACH.

It finds out which is the lowest and highest value ie low = 1 and high = 7.

Now it finds the middle value low + high / 2 ie 1 + 7 / 2 = 4

Then it checks IF mid > value

then it returns values 1 2 3 (hope u got it)

elseif mid < value.

then it returns values 5 6 7

elseif mid = value.

then it returns 4

endif.

it works as the same n required times

Hope this helps.

Neeraj

Hello experts:

Thank you in advance for all your replies.

READ TABLE VALUE WITH KEY KONPKEY BINARY SEARCH.

how does this statement work? would you please explain it with an example? cheers

couldn't thank you more.

Best regards.

Frank

4 REPLIES 4
Read only

Former Member
0 Likes
618

Hi,

check this link,

Thanks.

Read only

Former Member
0 Likes
619

Hello,

Make sure that the internal table that is used for READ statement should be sorted(either manually using SORT statement or using a SORTED internal table declaration)

Suppose there are values in internal table itab like

1

2

3

4

5

6

7

Here When u use READ TABLE itab WITH KEY (fieldname) = ' value ' BINARY SERACH.

It finds out which is the lowest and highest value ie low = 1 and high = 7.

Now it finds the middle value low + high / 2 ie 1 + 7 / 2 = 4

Then it checks IF mid > value

then it returns values 1 2 3 (hope u got it)

elseif mid < value.

then it returns values 5 6 7

elseif mid = value.

then it returns 4

endif.

it works as the same n required times

Hope this helps.

Neeraj

Read only

Former Member
0 Likes
618

Hi,

For example in your internal table you have 100 records.You want to Read 80th record.

READ TABLE ITAB WITH KEY num = '80' BINARY SEARCH.

Instead of searching till 80th record,First system will divide the reocrds half .

means 1- 50 and 51-100. Now system will compare the records

80 will be in between the 51-100.Again sytem wil divide the records half. 51-75 and 76-100.

Again system will divide the records again half 76-88 and 89 to 100.

Like that it will check with the number(80) .So searchnig will be fast.

NOTE. For correct result you have sort the internal table .Othewise you will get wrong result

Read only

Former Member
0 Likes
618

the command binary search will work if the no of records is very huge ...

it will read the value with the key value ...if it had 1000000 records if it is binary search in the first hit it checks in the first 50000 and if it is not available again it will search in the next 25000 records if it is not available in the next hit it will search in the next 12500 it is not available it will search in the next 6250 ..like this ..with in the 10 hits or just more it will catch the cxact record...it will improve the performance time of the program..

regards,

venkat.