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 search question

Former Member
0 Likes
675

Hi,

I want to use binary search in my code, just not sure, how to do it right.

Let's say I have an IT with columns A B C D.

I want to search this table first based on value in A and later based on B.

How should I sort this table?

>sort IT by A B

>read table with key A = '1' binary search

>read table with key B = 'X' binary search

or

>sort IT by A

>read table with key A = '1' binary search

>sort IT by B

>read table with key A = 'X' binary search

?

What happens, if I sort this table by A and let the system search based on value in B?

Thank you,

Olian

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
632

Try in SE30->Tips & Tricks...

5 REPLIES 5
Read only

Former Member
0 Likes
632

Olien,

prefer SAP help by just pressing F1 in binary search.

may we somtimes wrong but SAP never.

Amit.

Read only

Former Member
0 Likes
632

If you are in a loop with these searches you can better make copies of the IT for each sortfield

ITA[] = IT[].

sort ita by A.

read table with key A = '1' binary search

ITB[] = IT[].

sort itb by B.

read table with key B = 'X' binary search.

Read only

ThomasZloch
Active Contributor
0 Likes
632

>

>sort IT by A

>read table with key A = '1' binary search

>sort IT by B

>read table with key B = 'X' binary search

That is the way to go (I corrected the typo).

Binary search is only effective when the table is sorted by the search fields. It only makes sense though if you perform multiple reads after sorting (e.g. inside a loop), since sorting itself costs some processing time.

Thomas

Read only

Former Member
0 Likes
633

Try in SE30->Tips & Tricks...

Read only

0 Likes
632

ad you only have to add binary search when the amount of lines in the IT is for instance >10.000

for IT with lines between 0 and 1000 it is not really rewarding.

I had certainly improved some programs in performance with addind binary search to a IT with 30.000 lines which had to be looped through for 5000 times.

kind regards

arthur