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
1,078

What is th ediff between linear search and binary search?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,038

Hi,

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

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.

Linear -

You can apply linear or logarithmic scaling to the x- and y-axes by choosing the appropriate option in the Global Options dialog box.

Scaling the X-Axis

The x-axis must be a non-reference axis in order to specify logarithmic scaling. In addition, the scale range for the x-axis must have no negative values.

The X-axis scale option does not appear in the Global Options dialog box that appears for reference axis graphs with time line scaling.

Scaling the Y-Axis

The y-axis must be a non-reference axis in order to specify logarithmic scaling. In addition, the scale range for the y-axis must have no negative values.

*****do rewars if usefull

vijay

7 REPLIES 7
Read only

Former Member
0 Likes
1,038

Hi,

1 . this search takes lot of time , it searches one by one

2 . binary search is faster

suppose say there are 100 records in the internal table and you had written

read table itab with key city = 'hyderabad' binary search.

now it splits its search to 50 records and searches for H as Hyderabad starts with H whether it is in top 50 records or bottom 50 records

if it is in bottom 50 records , then again it will split to 25 each and searches for the next leeter and so on which simplifies the search

Reward points if useful..

Regards

Nilesh

Read only

Former Member
0 Likes
1,038

Hi Balu,

well, it's just a matter of algorithmics. In a sorted table, binary search is far more efficient than linear search. The first one needs O(log n) time, the second one needs O(n) time, if n is the amount of entries. This means, the time needed with the first algorithm is proportional to the logarithm (in base 2) of the number of entries, which can be dramatically smaller than linear time.

You may check this link: <a href="http://en.wikipedia.org/wiki/Binary_search">Binary search @ Wikipedia</a>

But remember: binary search only works when the source data set is sorted!

I hope this helps. Best regards,

Alvaro

Read only

Former Member
0 Likes
1,038

Hi..

Binary search is more faster and effective when data is sorted. The runtime is in logarithmic relation to the number of table entries.

where as Linear search will carry upto n times if the key is at the end.

It searches sequentially for the KEY in the data till it found.

Effective when KEY is at 1st position. Worst at KEY with last position.

Read only

Former Member
0 Likes
1,039

Hi,

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

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.

Linear -

You can apply linear or logarithmic scaling to the x- and y-axes by choosing the appropriate option in the Global Options dialog box.

Scaling the X-Axis

The x-axis must be a non-reference axis in order to specify logarithmic scaling. In addition, the scale range for the x-axis must have no negative values.

The X-axis scale option does not appear in the Global Options dialog box that appears for reference axis graphs with time line scaling.

Scaling the Y-Axis

The y-axis must be a non-reference axis in order to specify logarithmic scaling. In addition, the scale range for the y-axis must have no negative values.

*****do rewars if usefull

vijay

Read only

Bema
Active Participant
0 Likes
1,038

HI ,

Linear sear means , it will start searching from the first record.

To do the binary search, you have to sort the table first by key.

Then it will search with the value in the middle record.

if the value you are seaching is less than the value in the middle, it will search in the lower half of the table else in the upper part.

Again it compares with the middle record. it continues like this till it finds the value.

( binary search is a technique for finding a particular value in a sorted list. It makes progressively better guesses, and closes in on the sought value, by comparing an element halfway with what has been determined to be an element too low in the list and one too high in the list. A binary search finds the median element in a list, compares its value to the one you are searching for, and determines if it’s greater than, less than, or equal to the one you want. A guess that turns out to be too high becomes the new top of the list, and one too low the new bottom of the list. The binary search's next guess is halfway between the new list's top and bottom. Pursuing this strategy iteratively, it narrows the search by a factor 2 each time, and finds your value. )

Reward points , if helpful

Read only

Former Member
0 Likes
1,038

Hi,

A binary search algorithm (or binary chop) is a technique for finding a particular value in a linear array, by ruling out half of the data at each step, widely but not exclusively used in computer science. A binary search finds the median, makes a comparison to determine whether the desired value comes before or after it, and then searches the remaining half in the same manner. A binary search is an example of a divide and conquer algorithm (more specifically a decrease and conquer algorithm). From the above it is very important to SORT the table otherwise it might be worse than linear search.

Suppose you have 10 records and 6 th record is your search record.

In linear search we have to search record by record.

In binary search search can split the entire records into 2 groups and suppose if it finds the searching record in 1 group,it can ignore the other group.

Hope this info would be helpful.