what is a binary search ? and how it is useful in a sorted
internal table ?
Answer Posted / shweta upadhyay
Definition: 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.
SYNTAX : READ TABLE <itab> WITH KEY <k1> = <f1>... <kn> =
<fn> <result>
BINARY SEARCH.
CODE :
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.
| Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
Define dispatcher?
What is exception class and test class. How to use it?
Where do you create indexes in abap?
What is the significance of delivery class? : abap data dictionary
A function module can be called from a transaction screen outside an abap/4 program. State true or false. : abap modularization
What are the steps in creating screen ?
What are the different function modules used in sapscript?
What primary key and foreign key relationship
What are types of select statements?
How many types of data classes are there in sap abap? : abap data dictionary
what is bapi,badi,bdc,ale idocs?
How do you do bdc for a table control? : abap bdc
Difference between user exit and badis?
Find the scenario and give the result: a, b, c are employees, where a, b will get basic, hra, da. C gets basic and da. How to group for the allowances? : sap abap hr
How do you Export a session ?