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

tables

Former Member
0 Likes
1,196

hi guru's,

wat is the use of binary search? and where we can use this?provide me a sample code.if use full .................full points............urgetnt

thanx n regards,

lokanadhan.k

8 REPLIES 8
Read only

Former Member
0 Likes
1,154

You can use BINARY SEARC with READ statement.

It is used for faster searching. <b>It can be used only when table is sorted</b>

SORT ITAB2.

LOOP AT ITAB1.

READ TABLE ITAB2 WITH KEY FIELD1 = ITAB1-FIELD1 BINARY SEARCH.

IF SY-SUBRC = 0.

*Required code

ENDIF.

ENDLOOP.

Read only

Former Member
0 Likes
1,154

hi,

Read statement fetches the record from the internal table using implicit key or explicit key. When no key or index is specified for the search criteria the key in the WA of the internal table is used implicitly for searching. SAP recommends explicit search criteria. Here the key or index is explicitly specified which is used for searching the records from internal table. This is faster than an implicit search.

When reading a single record in an internal table, the READ TABLE WITH KEY is not a direct READ. Therefore, SORT the table and use READ TABLE WITH KEY BINARY SEARCH.

i) Do not use the following statement: -

Select matnr from mara

Into table i_mara

Where matnr in s_matnr.

Select matnr werks from marc

Into table i_marc

For all entries in i_mara

Where matnr eq i_mara-matnr.

Loop at I_mara.

-


Read table i_marc with key matnr = I_mara-matnr.

-


Endloop.

ii) Instead use the following statement: -

Select matnr from mara

Into table i_mara

Where matnr in s_matnr.

Select matnr werks from marc

Into table i_marc

For all entries in i_mara

Where matnr eq i_mara-matnr.

Sort I_marc by matnr.

Loop at I_mara.

-


Read table i_marc with key

matnr = I_mara-matnr

binary search.

-


Endloop.

***do reward if usefull

regards,

vijay

Read only

dev_parbutteea
Active Contributor
0 Likes
1,154

Hi,

it is used in place of simple read (if table contains more than 300 records)to increase performance.

example:

read table it_results_final assigning <fs_output_final>

with key ordkey = <fs_output_tab>-ordkey

itm_number = <fs_output_tab>-itm_number

binary search.

However you should ensure that the table is sorted by the fields with which you are doing the read else it will not work.

regards,

Sooness

Read only

Former Member
0 Likes
1,154

Hi,

There are different types of searches that you can use while searching data.

For example you have binary search, linear search etc.

Why use Binary search ?

Time taken to search the data is less with Binary search than linear search.

Binary search:

The primary requisite to apply binary search on an internal table is that table should be sorted by some field.

Now assume that you have some internal table and would like to search some particular field with some particular key , then you will sort the internal table and then read this table for a particular value

Code snippet:

  • Account Assignment in Purchasing Document

CLEAR ls_acc_ass_purdoc.

READ TABLE pa_acc_ass_purdoc INTO ls_acc_ass_purdoc

WITH KEY ebeln = ls_pur_doc_head_item-ebeln

ebelp = ls_pur_doc_head_item-ebelp

BINARY SEARCH.

Hope it helps.

Read only

Former Member
0 Likes
1,154

hi,

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> <result>

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.

<b>U CAN TRY BELOW CODE</b>

DATA: BEGIN OF line,

col1 TYPE i,

col2 TYPE i,

END OF line.

DATA itab LIKE STANDARD TABLE OF line.

DO 4 TIMES.

line-col1 = sy-index.

line-col2 = sy-index ** 2.

APPEND line TO itab.

ENDDO.

SORT itab BY col2.

READ TABLE itab WITH KEY col2 = 16 INTO line BINARY SEARCH.

WRITE: 'SY-SUBRC =', sy-subrc.

The output is:

SY-SUBRC = 0

Thanks,

Madhukar

Read only

Former Member
0 Likes
1,154

The search takes place as follows for the individual table types :

standard tables are subject to a linear search. If the addition BINARY SEARCH is specified, the search is binary instead of linear. This considerably reduces the runtime of the search for larger tables (from approximately 100 entries upwards). For the binary search, the table must be sorted by the specified search key in ascending order. Otherwise the search will not find the correct row.

sorted tables are subject to a binary search if the specified search key is or includes a starting field of the table key. Otherwise it is linear. If the specified search key is or inclucdes an starting field of the table key, the addition BINARY SEARCH can be specified for sorted tables, but has no effect.

For hashed tables, the hash algorithm is used if the specified search key includes the table key. Otherwise the search is linear. The addition BINARY SEARCH is not permitted for hashed tables.

Notes:

If a name field for naming a component comp is initial, the first line that fits the rest of the search key is read. If all name fields are initial, the first row of the internal table is read.

If the addition BINARY SEARCH is used, the READ statement executes an index access. This can therefore only be used for appropriately typed tables (generic type INDEX TABLE).

Even with a binary search (addition BINARY SEARCH is used for standard tables, automatically for sorted tables), if there are several hits (because of an incompletely specified search key or because there are duplicate entries in the table), then the first hit in terms of the sequence of the rows is always returned, that is, the row with the smallest index.

<b>example's</b>

Binary Search in Standard Tables 

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 = <f> <result> BINARY SEARCH.

and

READ TABLE <itab> WITH KEY <k1> = <f1> ... <kn> = <fn> <result> 
                                                       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. 

Example
DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.

DATA ITAB LIKE STANDARD TABLE OF LINE.

DO 4 TIMES.
  LINE-COL1 = SY-INDEX.
  LINE-COL2 = SY-INDEX ** 2.
  APPEND LINE TO ITAB.
ENDDO.

SORT ITAB BY COL2.

READ TABLE ITAB WITH KEY COL2 = 16 INTO LINE BINARY SEARCH.

WRITE: 'SY-SUBRC =', SY-SUBRC.

The output is: 

SY-SUBRC =    0

The program fills a standard table with a list of square numbers and sorts them into ascending order by field COL2. The READ statement uses a binary search to look for and find the line in the table where COL2 has the value 16. 

 

reward points if it is usefull ....

girish

Read only

S0025444845
Active Participant
0 Likes
1,154

Hi,

Binary search is very useful to imrove performances during read.

Iam giving u a simple example.

first we will sort the table with key which is used in reading n then we will read.

DATA: var1(6) TYPE c VALUE 'ABCDEF',

var2(5) TYPE c VALUE 'GHIJK',

result(12) TYPE c .

TYPES: BEGIN OF ty_vbak,

vbeln TYPE vbak-vbeln,

END OF ty_vbak,

BEGIN OF ty_vbap,

vbeln TYPE vbap-vbeln,

posnr TYPE vbap-posnr,

END OF ty_vbap.

DATA: it_vbak TYPE STANDARD TABLE OF ty_vbak,

it_vbap TYPE STANDARD TABLE OF ty_vbap,

it_out TYPE STANDARD TABLE OF ty_vbak,

wa_vbak TYPE ty_vbak,

wa_vbap TYPE ty_vbap,

wa_out TYPE ty_vbak.

SELECT vbeln INTO

TABLE it_vbak FROM vbak.

IF it_vbak[] IS NOT INITIAL.

SELECT vbeln posnr

into table it_vbap

from vbap

for all entries IN it_vbak

where vbeln = it_vbak-vbeln.

ENDIF.

sort: it_vbap by vbeln.

loop at it_vbak into wa_vbak.

read table it_vbap into wa_vbap

with key vbeln = wa_vbak-vbeln

binary search.

if sy-subrc is initial.

wa_out-vbeln = wa_vbap-vbeln.

endif.

clear: wa_vbap,wa_out,wa_vbak.

endloop.

regards,

sudha

reward points if useful

Read only

Former Member
0 Likes
1,154

select field1 from itab1.

SORT ITAB1 by field1.

LOOP AT ITAB1.

READ TABLE ITAB2 WITH KEY FIELD1 = ITAB1-FIELD1 BINARY SEARCH.

IF SY-SUBRC = 0.

*write what you want to do with the selected data here

ENDIF.

ENDLOOP.

Binary search is faster than normal search. The reason is, it uses a special searching algorithm. First you need to sort the required field. for example, the itab1-field1 in the above code has 1,2,3,4,5

and your itab2 has field1 value as 4.

so first, the program takes the middle value in itab1, say 3.

then it compares this with itab2-field. now 4 is greater than 3, so the program knows the value could be greater than 3, that is 4 or 5. it proceeds like this and finds the record

Thanks

Arun