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: 

binary search does not working in descending order list

suba1994hit
Explorer

I was experimenting on binary search.

I found that binary search is perfectly working on ascending order internal table,

but does not work in descending order internal table.

Why binary search does not support descending order internal table?

1 ACCEPTED SOLUTION

omer_sakar
Participant
0 Kudos

Hi Subrata,

It is about nature of binary search,not only Sap. You should search and learn about binary search ,for example : https://www.tutorialspoint.com/data_structures_algorithms/binary_search_algorithm.htm

7 REPLIES 7

horst_keller
Product and Topic Expert
Product and Topic Expert

Because it is designed and documented to work on ascending sort order.

0 Kudos

You just saved my day!!!!

This is the correct answer for this query and not the one which is marked as "Best Answer" here.

In my academics what I had learnt is BINARY Search works well for Ascending and Descending Order.

But in ABAP we have an exception and that is it works for Ascending Order only.

Thanks,

Sijin

omer_sakar
Participant
0 Kudos

Hi Subrata,

It is about nature of binary search,not only Sap. You should search and learn about binary search ,for example : https://www.tutorialspoint.com/data_structures_algorithms/binary_search_algorithm.htm

pokrakam
Active Contributor
0 Kudos

Erm, no. The "nature of binary search" has nothing to do with the order.

There's nothing stopping you from coding a binary search function yourself that supports descending order.

0 Kudos

Of course there is nothing to do stop your going further but when we use a bicycle , we could not complain that why it doesnt fly.The nature of those things ,we should go to a ticket for airplane.

pokrakam
Active Contributor

We can sort ascending and descending, so it is perfectly reasonable to question if we can binary search both variants. Algorithmically it is exactly the same process, nothing unnatural about that, and no flying involved. SAP has decided not to implement it, it's as simple as that.

prabhu_04
Explorer
0 Kudos

Only Ascending is possible.

Descending is not possible means In binary search ( like 1 2 3 4 5 6 7 ) I need to search 7

In Descending order ( 7 6 5 4 3 2 1 ) it picked 4 it is middle so its checking greaterthan first 3 values it is not considering . Its search next three values. next three value is Min number of 4