Quantcast
Channel: SCN : Blog List - ABAP Development
Viewing all articles
Browse latest Browse all 943

Linear search vs binary search in internal table

$
0
0

Hi,

 

Here will show difference between the linear search vs binary search in the internal table.

 

First let me give me some intro about the liner search linear search or sequential search is a method for finding a particular value in a list, that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found.

 

Binary search: It finds the position of the specified value in the a sorted array

 

 

For ex: linear search in a internal table

 

READ TABLE ITAB INTO WA WITH KEY K = 'X'.

 

Binary search in internal table:

 

READ TABLE ITAB INTO WA WITH KEY K = 'X' BINARY SEARCH.

 

If internal tables are assumed to have many (>20) entries, a linear search through all entries is very time-consuming. Try to keep the table ordered and usebinary search or used a table of type SORTED TABLE. If TAB has n entries, linear search runs in O( n ) time, whereas binary search takes only O( log2( n ) ).

 

And when you measure the runtime of the liner search is takes 77 microseconds and for binary search is 0 micro seconds


Viewing all articles
Browse latest Browse all 943

Trending Articles