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,394

Hello,

I had a small query , its regarding read table with binary search.

My query is can we use read table with binary search on an internal table that has been sorted in descending order?

Thanks,

Alok.

10 REPLIES 10
Read only

Former Member
0 Likes
1,231

It can be used....

But it would cause extremely Severe performance issues...better to avoid...

Read only

0 Likes
1,231

Hi SubZero,

I tried using it with descending , but it doesnt give the same results, is it that only ascending works?

and what would be the kind of performance issues incase of descending.

Thanks,

Alok.

Edited by: Alok Navare on Apr 8, 2009 7:47 AM

Read only

0 Likes
1,231

Alok

Well the binary search algorithm itself asks for the keys to be sorted in ascending order... So it doesnt necessarily depend whether its ABAP or C or java..whatever....

I am not sure how it affects .. but i think when sorted in ascending order, performance is O(n) and when in descending order its of O(n square )......

But if its not working when its sorted in descending , then no point in using it buddy

Read only

Peranandam
Contributor
0 Likes
1,231

HI ,

yes, the fields you are using with key addition in read statement shoud be in sort (it must be sorted in ascending order)

you need to sort fiels which are using with key addition

ex.

itab having fiels a, b,c ,d, e , f, g.

here you are goint to read based on three fieds a, b, c.

sort itab by a b c.

read table itab with key a = 'aaa'

b = 'bbbb'

c = 'ccc'

binary search.

Regards,

Peranandam

Edited by: peranandam chinnathambi on Apr 8, 2009 7:49 AM

Edited by: peranandam chinnathambi on Apr 8, 2009 8:21 AM

Read only

faisalatsap
Active Contributor
0 Likes
1,231

Hi, Alok,

You must Sort your Internal Table in ASCENDING order for BINARY SEARCH

Best Regards,

Faisal

Read only

Former Member
0 Likes
1,231

Hi,

We can use for ascending and decending.

if u r getting different values means, The internal table may have multiple records with the key u r passing , If the table has only one record then the result will be same..

Performwnce is also same in both the cases..

Kiran

Edited by: kiran vempati on Apr 8, 2009 8:00 AM

Read only

0 Likes
1,231

Sorry, Kiran

It must be ascending order test the following code it is not working with DESCENDING but will work when Chang to ASCENDING.

TYPES: BEGIN OF ty_test,
  no TYPE i,
  END OF ty_test.
DATA: it TYPE STANDARD TABLE OF ty_test WITH HEADER LINE.

DO 1000 TIMES.
  it-no = sy-index.
  APPEND it.
ENDDO.

SORT it BY no DESCENDING.

READ TABLE it WITH KEY no = 838 BINARY SEARCH.
IF sy-subrc EQ 0.
  WRITE: it-no.
ENDIF.

Best Regards,

Faisal

Read only

0 Likes
1,231

Thanks Faisal,

U r correct... Sorry for my wrong answer...

Read only

Former Member
0 Likes
1,231
data: itab like standard table of spfli,
      fs type spfli.

      select * from spfli into table itab.

sort itab by carrid descending .        

read table itab into fs with key carrid = 'AA' binary search.

write : fs-connid.

sort itab by carrid.        

read table itab into fs with key carrid = 'AA' binary search.

write : fs-connid.

Check the difference of the result beacuse binary search will only work with ascending sort order.

Binary search works on Divide and Rule methods. after dividing It will check with the middle value if its lesser than the searched element than it goes to right half else it goes to left half.

Hence its mandt that the search filed of the table should be in Ascending order.

Regards,

Gurpreet

Read only

Former Member
0 Likes
1,231

If you are using the Binary search in Read statement you should sort the internal table before read statement. Otherwise it won't give correct answer.

Regards,

Joan