Application Development 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: 

hai friends ...canu help me this problem.

Former Member
0 Kudos
112

hai,thanks for reding my article..

can any body tell me what is exactly..binary search.? when it is manditory to use..

i heard that deafault search is linear search.and if we have more than 100 records we use binary search....so what happens if i use binary search for less than 100 records..

every anwer is awarded

regards

satish.v

1 ACCEPTED SOLUTION

Former Member
0 Kudos
76

Hi,

If no. of records are more, binary search is a good option in performance point of view.

Process:

1.Initially sort the data.

2. Apply binary search .

it divides the data into 2 equal parts and compares the value.

if the value is less than the middle value, it consider the first half, else consider the 2nd half.

ex: 1 2 34 5 6 7 8 9 10

Search value is 9

the mid value is 5. As 9 is greater than 5, it takes the 2nd half.

i.e., 6 7 8 9 10.

once again take the mid value. i.e., 8.

8 is less than 9. hence consider the second half in this 5 values.

i.e.,. 9 10.

and it proceeds further the same.

this process is very fast than linear search.

Hope this is clear.

9 REPLIES 9

Former Member
0 Kudos
76

Hi,

Binary serch is a search technique used when there are large number of records in the internal table.

Before using this addition the table has to be sorted by the field to be used as key in the READ statement.

During run time the contents of the table are divided into two and then searched for the record which we require through read statement.

This reduces the system time in detecting the record from the internal table

Former Member
0 Kudos
76

hi,

the following threads will answer u r question

http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb373d358411d1829f0000e829fbfe/frameset.htm

regards,

pavan

REWARD if USEFUL

Former Member
0 Kudos
77

Hi,

If no. of records are more, binary search is a good option in performance point of view.

Process:

1.Initially sort the data.

2. Apply binary search .

it divides the data into 2 equal parts and compares the value.

if the value is less than the middle value, it consider the first half, else consider the 2nd half.

ex: 1 2 34 5 6 7 8 9 10

Search value is 9

the mid value is 5. As 9 is greater than 5, it takes the 2nd half.

i.e., 6 7 8 9 10.

once again take the mid value. i.e., 8.

8 is less than 9. hence consider the second half in this 5 values.

i.e.,. 9 10.

and it proceeds further the same.

this process is very fast than linear search.

Hope this is clear.

0 Kudos
76

hi ramesh i am going to award full points for u but just tell me what will happen if i use binary search for less than 100 records..? why linear search is preffered for less than 100 records..bec i understood clearly binary ..though 100 records are there it will discount it..to 50..aagin 25..like that..

regards

satish.v

0 Kudos
76

Hi,

If no. of records are 100 and the value required is in 2nd place,

it takes the same time as it takes for 1000 records.

in this case if u use linear search, with the first comparision itself u will get the result.

Hope its clear.

0 Kudos
76

hi ramesh hink once though it is in the second place...u r case is ok..so if it is 70 postion whats the matter..? linear search will take almost 70 counts..at that time binary search takes only 5 steps to reach

what u say..?

Former Member
0 Kudos
76

HI,

suppose if u have numbers from 1 to 100.then u are trying to search for number 71 then in case of linear search it will search one by one.suppose if they are in ascending order 1,2,3....71,...1000 then for finding 71 it has to check for 70 times and 71st is the success.

in case of binary search we will make them either in ascending or descending order.in case of binary search it will divide the numbers into two sets,1-50 and 51-100,71 is there in second set so it will divide it into two sets again 51-75,75-100.76 is there in second set so it will go on like this and number of times searching is less.so,it is good performance wise.

rgds,

bharat.

Former Member
0 Kudos
76

hai

Binary Search will devide ur number of records into half and then start searching..

if ur itab has 500 records the search starts from # 251 to 500 one side and 250 to 1 on the other side.. so its like dividing and accomplishing our task of search.

normal search goes by 1 to 500 only ... so binary search is more efficiant.. but u need to sort the table to use binary search

Former Member
0 Kudos
76

hi satish,

When a programmer uses the read command, the table is sequentially searched. This slows down the processing. Instead of this, use the binary search addition. The binary search algorithm helps faster search of a value in an internal table. It is advisable to sort the internal table before doing a binary search. Binary search repeatedly divides the search interval in half. If the value to be searched is less than the item in the middle of the interval, the search is narrowed to the lower half, otherwise the search is narrowed to the upper half.

Not Recommended

Read table int_fligh with key airln = ?LF?.

Recommended

Read table int_fligh with key airln = ?LF? binary search.

Linear search vs. binary

READ TABLE TAB WITH KEY K = 'X'.

READ TABLE TAB WITH KEY K = 'X' BINARY SEARCH.

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.

REPORT demo_int_tables_read_index_bin.

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.