<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: linear search and binary search in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/linear-search-and-binary-search/m-p/3208228#M764787</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;There are 9 numbers , we have to find 7&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1 2 3 4 5 6 7 8 9&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Linear search :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;we proceed 1 by 1 : it takes 7 iterations&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Binary search:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;we take middle value( 5) compare with 7  now we are left with&lt;/P&gt;&lt;P&gt;6 7 8 9&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;we compare (8) and (7) with  7  ...&lt;/P&gt;&lt;P&gt;Thus process completed in 2 iteraions&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;read table itab into wa with key &amp;lt;&amp;gt; binary search.&lt;/P&gt;&lt;P&gt;table should be in asc order&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 20 Dec 2007 09:04:13 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2007-12-20T09:04:13Z</dc:date>
    <item>
      <title>linear search and binary search</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/linear-search-and-binary-search/m-p/3208226#M764785</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;can any one tell me what is linear and binary search in detail.&lt;/P&gt;&lt;P&gt;and what is the difference between them .&lt;/P&gt;&lt;P&gt;which one is useful in coding.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&amp;amp;Regards,&lt;/P&gt;&lt;P&gt;S.GangiReddy.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Dec 2007 08:58:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/linear-search-and-binary-search/m-p/3208226#M764785</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-12-20T08:58:52Z</dc:date>
    </item>
    <item>
      <title>Re: linear search and binary search</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/linear-search-and-binary-search/m-p/3208227#M764786</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;BINAY SEARCH in READ will definitely increase performance.But you need to SORT the internal table with all fields specifying in your READ statement.&lt;/P&gt;&lt;P&gt;Use the following statement before READ statement with BINARY SEARCH.&lt;/P&gt;&lt;P&gt;SORT TKOMV BY KNUMV KPOSN KSCHL. &lt;/P&gt;&lt;P&gt;READ TABLE TKOMV WITH KEY KNUMV = GT_VBAK-KNUMV&lt;/P&gt;&lt;P&gt;KPOSN = GT_VBAP-POSNR&lt;/P&gt;&lt;P&gt;KSCHL = 'PR00'&lt;/P&gt;&lt;P&gt;BINARY SEARCH.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;binary search is one of the fastest way to find the record exists in the internal table.&lt;/P&gt;&lt;P&gt;TO use BINARY SEARCH, you have to SORT the internal table in ASCENDING/DESCENDING ORDER. Then only you will get the exact results.&lt;/P&gt;&lt;P&gt;For more detail you can refer to below threads:&lt;/P&gt;&lt;P&gt;&lt;A class="jive_macro jive_macro_message" href="https://community.sap.com/" __jive_macro_name="message" modifiedtitle="true" __default_attr="1084944"&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A class="jive_macro jive_macro_message" href="https://community.sap.com/" __jive_macro_name="message" modifiedtitle="true" __default_attr="762027"&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If the addition BINARY SEARCH is specified, the search is binary instead of linear. &lt;/P&gt;&lt;P&gt;This considerably reduces the runtime of the search for larger tables (from approximately 100 entries upwards).&lt;/P&gt;&lt;P&gt;For the binary search, the table must be sorted by the specified search key in ascending order. &lt;/P&gt;&lt;P&gt;Otherwise the search will not find the correct row.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Dec 2007 09:01:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/linear-search-and-binary-search/m-p/3208227#M764786</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-12-20T09:01:40Z</dc:date>
    </item>
    <item>
      <title>Re: linear search and binary search</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/linear-search-and-binary-search/m-p/3208228#M764787</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;There are 9 numbers , we have to find 7&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1 2 3 4 5 6 7 8 9&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Linear search :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;we proceed 1 by 1 : it takes 7 iterations&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Binary search:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;we take middle value( 5) compare with 7  now we are left with&lt;/P&gt;&lt;P&gt;6 7 8 9&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;we compare (8) and (7) with  7  ...&lt;/P&gt;&lt;P&gt;Thus process completed in 2 iteraions&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;read table itab into wa with key &amp;lt;&amp;gt; binary search.&lt;/P&gt;&lt;P&gt;table should be in asc order&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Dec 2007 09:04:13 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/linear-search-and-binary-search/m-p/3208228#M764787</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-12-20T09:04:13Z</dc:date>
    </item>
    <item>
      <title>Re: linear search and binary search</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/linear-search-and-binary-search/m-p/3208229#M764788</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;linear search is nothing but processing sequntially&lt;/P&gt;&lt;P&gt;one by one&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;binary search&lt;/P&gt;&lt;P&gt;Binary search helps incase of standard tables..where you have to sort the internal table with the field and use that field to search the internal table with binary search..&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Dec 2007 09:04:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/linear-search-and-binary-search/m-p/3208229#M764788</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-12-20T09:04:15Z</dc:date>
    </item>
    <item>
      <title>Re: linear search and binary search</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/linear-search-and-binary-search/m-p/3208230#M764789</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Linear search:&lt;/P&gt;&lt;P&gt;                    Which will search line by line.&lt;/P&gt;&lt;P&gt;Binary Search:&lt;/P&gt;&lt;P&gt;                     which will directly go to n&lt;EM&gt;1/2 record if it matchs the condition then ok otherwise if the N&lt;/EM&gt;1/2  record is greater than the searching value it will ignores the 2nd half of the records, again from the rest of 1st half records it will calculate the middle record and then it will checks for the condition, in this way it will work, it means at a single iteration it will eliminates half of the records from the search criteria.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Note:- for binary search the table should be in Sorted (ASCENDING) order.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Reward if useful.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Sreeram.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Dec 2007 09:08:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/linear-search-and-binary-search/m-p/3208230#M764789</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-12-20T09:08:15Z</dc:date>
    </item>
    <item>
      <title>Re: linear search and binary search</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/linear-search-and-binary-search/m-p/3208231#M764790</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;READ TABLE &amp;lt;itab&amp;gt; WITH KEY &amp;lt;k1&amp;gt; = &amp;lt;f1&amp;gt;... &amp;lt;kn&amp;gt; = &amp;lt;fn&amp;gt; &amp;lt;result&amp;gt;  BINARY SEARCH.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The standard table must be sorted in ascending order by the specified search key. The BINARY SEARCH addition means that you can access an entry in a standard table by its key as quickly as you would be able to in a sorted table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;REPORT demo_int_tables_read_index_bin.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DATA: BEGIN OF line,&lt;/P&gt;&lt;P&gt;        col1 TYPE i,&lt;/P&gt;&lt;P&gt;        col2 TYPE i,&lt;/P&gt;&lt;P&gt;      END OF line.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DATA itab LIKE STANDARD TABLE OF line.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DO 4 TIMES.&lt;/P&gt;&lt;P&gt;  line-col1 = sy-index.&lt;/P&gt;&lt;P&gt;  line-col2 = sy-index ** 2.&lt;/P&gt;&lt;P&gt;  APPEND line TO itab.&lt;/P&gt;&lt;P&gt;ENDDO.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SORT itab BY col2.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;READ TABLE itab WITH KEY col2 = 16 INTO line BINARY SEARCH.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;WRITE: 'SY-SUBRC =', sy-subrc.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The output is:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SY-SUBRC =    0&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Linear search use sequential search means each and every reord will be searched to find. so it is slow.&lt;/P&gt;&lt;P&gt;Binary search uses logrim for searching. Itab MUST be sorted on KEY fields fro binary search. so it is very fast.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The search takes place as follows for the individual table types : &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;standard tables are subject to a linear search. If the addition BINARY SEARCH is specified, the search is binary instead of linear. This considerably reduces the runtime of the search for larger tables (from approximately 100 entries upwards). For the binary search, the table must be sorted by the specified search key in ascending order. Otherwise the search will not find the correct row. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;sorted tables are subject to a binary search if the specified search key is or includes a starting field of the table key. Otherwise it is linear. The addition BINARY SEARCH can be specified for sorted tables, but has no effect. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For hashed tables, the hash algorithm is used if the specified search key includes the table key. Otherwise the search is linear. The addition BINARY SEARCH is not permitted for hashed tables.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Binary search must be preffered over linear sarch.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Hope this is helpful, Do reward.&lt;/STRONG&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Dec 2007 09:13:49 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/linear-search-and-binary-search/m-p/3208231#M764790</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-12-20T09:13:49Z</dc:date>
    </item>
    <item>
      <title>Re: linear search and binary search</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/linear-search-and-binary-search/m-p/3208232#M764791</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Linear Search:&lt;/STRONG&gt; : This is sequential search.&lt;/P&gt;&lt;P&gt; for example: we have 4 sales doc numbers like&lt;/P&gt;&lt;P&gt; 500001&lt;/P&gt;&lt;P&gt; 500012&lt;/P&gt;&lt;P&gt; 501234&lt;/P&gt;&lt;P&gt; 514444&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;in the above list we have to  found '501234'.&lt;/P&gt;&lt;P&gt;so linear search will start search from 500001 next 500012 &lt;/P&gt;&lt;P&gt;next 501234.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Binary Search&lt;/STRONG&gt;: It will follow the different mechanism.&lt;/P&gt;&lt;P&gt;First it will make the list '1/2'.&lt;/P&gt;&lt;P&gt;It means it will break the list into two parts.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If the element is found in the first part. Number of repitions are less.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So use of binary search is better.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Anil&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Dec 2007 10:21:49 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/linear-search-and-binary-search/m-p/3208232#M764791</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-12-20T10:21:49Z</dc:date>
    </item>
    <item>
      <title>Re: linear search and binary search</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/linear-search-and-binary-search/m-p/3208233#M764792</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thanks a lot for giving wondraful example.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&amp;amp;Regards,&lt;/P&gt;&lt;P&gt;S.Gangi Reddy.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Dec 2007 12:27:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/linear-search-and-binary-search/m-p/3208233#M764792</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-12-20T12:27:52Z</dc:date>
    </item>
  </channel>
</rss>

