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

Former Member
0 Likes
632

Would the following binary search produce correct results?

Internal table IT is sorted by field1 field2 field3

read table IT with key field2 = 'X' field3 = 'Y'

I am not sure this is correct since the read statement makes no

reference to field1.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
614

Binary search always use divide and conquer method:

So ex:

Field1 Field2 Field3

ZX BC ZA

XY DE YH

ZA AA ZA

After Sort on Field1 Field2 Field3

result will be:

XY DE YH

ZA AA ZA

ZX BC ZA

If we dont use Field on in read the result will not be accurate.

IF you want to use field2 .

USE :

Sort itab by field2 field3.

Read table itab with key field2 field3 BINARY SEARCH.

Hope this resolves your query.

Regards,

Gurpreet

5 REPLIES 5
Read only

former_member156446
Active Contributor
0 Likes
613

use all the fields you used to sort , to read as well

this will:

read table IT with key field1 =  'X' field2 = 'X' field3 = 'Y'

Read only

Former Member
0 Likes
615

Binary search always use divide and conquer method:

So ex:

Field1 Field2 Field3

ZX BC ZA

XY DE YH

ZA AA ZA

After Sort on Field1 Field2 Field3

result will be:

XY DE YH

ZA AA ZA

ZX BC ZA

If we dont use Field on in read the result will not be accurate.

IF you want to use field2 .

USE :

Sort itab by field2 field3.

Read table itab with key field2 field3 BINARY SEARCH.

Hope this resolves your query.

Regards,

Gurpreet

Read only

Former Member
0 Likes
613

It depends on what you mean by "correct". Why not try it and see if you get the results you want?

Rob

Read only

Former Member
0 Likes
613

You could determine the result by running this. You'll see that it returns the first row. It is not, by the way, a binary search. And there is no way to make it binary unless you provide the entire key.

TYPES:  BEGIN OF _ty_1,
        field1(1)  TYPE c,
        field2(1)  TYPE c,
        field3(1)  TYPE c.
TYPES:  END   OF _ty_1.


DATA:
  g_t_tab TYPE TABLE OF _ty_1,
  g_s_line TYPE _ty_1.

g_s_line-field1 = 'A'.
g_s_line-field2 = 'X'.
g_s_line-field3 = 'Y'.
APPEND g_s_line TO g_t_tab.

g_s_line-field1 = 'B'.
g_s_line-field2 = 'X'.
g_s_line-field3 = 'Y'.
APPEND g_s_line TO g_t_tab.

g_s_line-field1 = 'C'.
g_s_line-field2 = 'X'.
g_s_line-field3 = 'Y'.
APPEND g_s_line TO g_t_tab.

SORT g_t_tab BY field1 field2 field3.

READ TABLE g_t_tab WITH KEY field2 = 'X' field3 = 'Y' into g_s_line.

WRITE:/ g_s_line-field1,sy-subrc.

Read only

Former Member
0 Likes
613

Hi,

there is thumb rule when using a BINARY SEARCH.

when ever you are using a Binary Search for a read SORT the table before read with what ever fields you want, and use the same fields in sequence what you used for sorting ,

if you use different keys Binary search Fails but it works sometimes not always...

like

sort IT by field1 field2 field3

read table IT with key field1 = 'X' field2 = 'X' field3 = 'Y'

if you are using only field2 and field3 as key then sort internal table by field2 and field3 only.

Regards,

vinayaka