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 Function

Former Member
0 Kudos
4,635

Hi All,

Pasted below is a code in which i am not able to understand the Binary Search function in this code.

Please explain.

READ TABLE LT_HIENODE

WITH KEY IOBJNM = LS_HIENODE-IOBJNM

NODENAME = LS_HIENODE-NODENAME

BINARY SEARCH TRANSPORTING NO FIELDS

1 ACCEPTED SOLUTION
Read only

Former Member
0 Kudos
2,305

binary search is one of the fastest way to find the record exists in the internal table.

TO use BINARY SEARCH, you have to sort the internal table in ASCENDING ORDER. Then only you will get the exact results.

what binary search will do is :

example :

suppose you have 1,8,6,4,3 in your internal table.

now you want to find whether 2 is there in the table or not,

so first you have to do sort.

then it becomes : 1,3,4,6,8.

binary search will now starts as follows :

if total no of records:

if its even

it takes total no of records / 2 .

if its odd :

it takes ( total no of records + 1) / 2 .

in our case 3.

so now it will check the value at 3rd position.

it will compare the value in 3rd position with the value we are searching.

in our case we are searching for 3. so 3 <> 4.

so then it will compare 3 is smaller or bigger than the 3rd position value ..

for our case its less than 3rd position value , so it will consider 1st half part & leaves next half part.

again this do the same procedure..

got any idea ???

regards

srikanth

13 REPLIES 13
Read only

FredericGirod
Active Contributor
0 Kudos
2,305

Hi Sajan,

what didn't you understand ?

the binary search is a dycotomic search ...

the TRANSPORTING NO FIELDS have nothing to do with the search, that's means the read is only here to check if the criteria match in the table.

Rgd

Frédéric

Read only

Clemenss
Active Contributor
0 Kudos
2,305

Hi,

easy: This only means that there is (SY-SUBRC = 0) or isn't (SY-SUBRC = 4 or SY-SUBRC = 8 for empty table) an entry for the specified key values.

the addition TRANSPORTING NO FIELDS makes it extremely fast to check if an entry exists or not.

In many cases the field data are of no interest.

regards,

Clemens

Read only

Former Member
0 Kudos
2,305

Hi Clemens,

thanks for u r reply.

I find this statement in almost all ABAP codes (SY-SUBRC = 0) etc...

can u also explain what is use of using tht statement.

Sajan.M

Read only

Former Member
0 Kudos
2,305

if your intension is to just check a record exists in the itab & no need of its values in that record.

you use

READ TABLE ITAB.... TRANSPORTING NO FIELDS.

This will improve your process time. as this statement does not transfer that particular record contents to header line.so obviously its more fast.

if you dont use TRANSPORTING .. addition in the READ TABLE Statement,

if the read is success, all the fields of that record will be transported to header line.

this transporting of all fields from body to header line takes lit bit time.(may be very few micro seconds)

regards

srikanth

Read only

Former Member
0 Kudos
2,305

Hi Srikanth,

I find this statement in almost all ABAP codes (SY-SUBRC = 0) etc...

can u explain this also as to what is need for using tht statement.

Sajan.M

Read only

Former Member
0 Kudos
2,305

hi,

i already mentioned in the earlier post, why we need this ?? TRANSPORTING NO FIELDS..

simply i can say to improve the efficiency of the program.

IF you mention TRANSPORTING NO FIELDS, system will not spent time on transferring that record contents(field vaues )from table body(for example from line 100) to header.

if you have NOT mention about any thing about transporting,

system will use few seconds time, to transfer that record fields to header line ..

Message was edited by: Srikanth Kidambi Maruthi

Read only

Former Member
0 Kudos
2,305

Thats fine srikanth,

This sy-subrc = 0 is something i keep seeing in ABAP pretty frequently. Its used some thing like a IF statement. For eg: IF sy-subrc = 0 then a condition is executed.

I dont know wht tht IF statement means??

Pls clarify!

Sajan.M

Read only

Former Member
0 Kudos
2,305

every statement will have either successfully executed or failed result.

if its successful..

you want to do some work

if its fail.

you will display error message.

to accomplish the above in SAP ABAP,

you have to depend on the value of SYST-SUBRC.

This will have the last statement return status.

i mean

if the last statement executed successfully this will have 0 " in SAP 0 is SUCCESS & Other than 0 is FAILURE

IF SY-SUBRC = 0.

*--SUCESSS

ELSE.

*--What ever the value of SUBRC,if its not equal to 0,its fail

endif.

Message was edited by: Srikanth Kidambi Maruthi

Read only

Former Member
2,305

read table is to read a specific record from the internal table(body).

the addition of BINARY SEARCH to this read table will improve the efficienty of READ statement of that particular record.

if you mention TRANSPORTING NO FIELDS,means no value come to header line of that internal table ,just you will come to know any record is there in that internal table with the key passed to read table statement.

in this case you can check sy-subrc value

if its 0, read statement is success

else fail

regards

srikanth

Read only

Former Member
0 Kudos
2,305

Hi Rajagopal,

Binary search will half the search area in every iteration.

<b>Explaination:</b>

Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.

Regards,

Raj

Read only

Former Member
0 Kudos
2,306

binary search is one of the fastest way to find the record exists in the internal table.

TO use BINARY SEARCH, you have to sort the internal table in ASCENDING ORDER. Then only you will get the exact results.

what binary search will do is :

example :

suppose you have 1,8,6,4,3 in your internal table.

now you want to find whether 2 is there in the table or not,

so first you have to do sort.

then it becomes : 1,3,4,6,8.

binary search will now starts as follows :

if total no of records:

if its even

it takes total no of records / 2 .

if its odd :

it takes ( total no of records + 1) / 2 .

in our case 3.

so now it will check the value at 3rd position.

it will compare the value in 3rd position with the value we are searching.

in our case we are searching for 3. so 3 <> 4.

so then it will compare 3 is smaller or bigger than the 3rd position value ..

for our case its less than 3rd position value , so it will consider 1st half part & leaves next half part.

again this do the same procedure..

got any idea ???

regards

srikanth

Read only

0 Kudos
2,305

Hi srikanth,

tht was a brilliant reply.

Thanks

Sajan.M

Read only

Former Member
0 Kudos
2,305

let me know the standard primary key tables in standard internal table