<?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: Loop on sorted table and binary search in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-on-sorted-table-and-binary-search/m-p/11142583#M1906288</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;sorry, but although this has been posted ages ago, I must disagree on 3):&lt;/P&gt;&lt;P&gt;When itab_sortd is declared as SORTED TABLE any access (LOOP, READ,...) will be a binary as soon as an initial part of a key is used.&lt;/P&gt;&lt;P&gt;READ TABLE itab_sortd WITH TABLE KEY matnr = 'XXXX' &lt;/P&gt;&lt;P&gt;READ TABLE itab_sortd WITH KEY matnr = 'XXXX' &lt;/P&gt;&lt;P&gt;will both be executed as binary search, because matnr is the first field of primary key. (same applies for secondary keys...)&lt;/P&gt;&lt;P&gt;It is ONLY the STANDARD TABLE that needs explicit addition BINARY SEARCH.&lt;/P&gt;</description>
    <pubDate>Tue, 07 Jan 2020 10:30:32 GMT</pubDate>
    <dc:creator>uba_heraeus</dc:creator>
    <dc:date>2020-01-07T10:30:32Z</dc:date>
    <item>
      <title>Loop on sorted table and binary search</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-on-sorted-table-and-binary-search/m-p/11142580#M1906285</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Gurus, &lt;/P&gt;&lt;P&gt;I have few queries about sorted tables &lt;SPAN __jive_emoticon_name="happy" __jive_macro_name="emoticon" class="jive_emote jive_macro" src="https://community.sap.com/1291/images/emoticons/happy.gif"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;i have a sorted internal table, &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;itab_sortd type sorted table with non-unique key MATNR WERKS.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Loop at itab_sortd into wa_srtd where matnr = 'xxxx'.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .....&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Endloop.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I use both the keys in where statement i can achieve a binary search.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;1.&lt;/STRONG&gt; but what if i am not using the whole key? Will it still do a binary search as i use leftmost part of the key.&lt;/P&gt;&lt;P&gt;I have read that if you use the leftmost part of the key, we can achieve a binary search on sorted tables.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;2.&lt;/STRONG&gt; Also how about loop with all key fields + additional fields? &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.g &lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 13.3333330154419px;"&gt;itab_sortd type sorted table with non-unique key MATNR WERKS.&lt;/STRONG&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;STRONG&gt;Loop at itab_sortd into wa_srtd where MATNR = 'xxxx' WERKS= 'yyyy' PSTNK = 'zzzz'.&lt;/STRONG&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .....&lt;/STRONG&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;STRONG&gt;Endloop&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;3.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;STRONG style="font-size: 13.3333330154419px;"&gt;itab_sortd type sorted table with non-unique key MATNR WERKS.&lt;/STRONG&gt; &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;READ TABLE itab_sortd WITH KEY matnr = 'XXXX'.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Will the above statement result in a binary search ?&lt;/P&gt;&lt;P&gt;Thanks in advance,&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Jul 2015 11:01:18 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-on-sorted-table-and-binary-search/m-p/11142580#M1906285</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2015-07-07T11:01:18Z</dc:date>
    </item>
    <item>
      <title>Re: Loop on sorted table and binary search</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-on-sorted-table-and-binary-search/m-p/11142581#M1906286</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi Rojer,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1) You are right.&lt;/P&gt;&lt;P&gt;2) Binary search will work only for&amp;nbsp; keys mentioned in the table key.&lt;/P&gt;&lt;P&gt;3) Yes.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Jul 2015 11:37:54 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-on-sorted-table-and-binary-search/m-p/11142581#M1906286</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2015-07-07T11:37:54Z</dc:date>
    </item>
    <item>
      <title>Re: Loop on sorted table and binary search</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-on-sorted-table-and-binary-search/m-p/11142582#M1906287</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Rojar ,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Please find my answers to your question&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1) Yes . As alread the table is sorted with MATNR and WERKS , if you use just MATNR it will act as binary serach only.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2) Please find the below syntax stmt for your requirement (L&lt;SPAN style="color: #333333; font-size: 11.9999990463257px;"&gt;oop with all key fields + additional fields&lt;/SPAN&gt;)&lt;/P&gt;&lt;P&gt;itab_sortd type sorted table with non-unique key MATNR WERKS.&lt;/P&gt;&lt;P&gt;Loop at itab_sortd into wa_srtd where MATNR = 'xxxx' &lt;STRONG&gt;and&lt;/STRONG&gt; WERKS= 'yyyy' &lt;STRONG&gt;and&lt;/STRONG&gt; PSTNK = 'zzzz'.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .....&lt;/P&gt;&lt;P&gt;Endloop&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;3) No. &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; For the statements mentioned by you above , only the Linear Search will happen since you have not mentioned the complier to use the BINARY SEARCH technique for reading.You need to explicity mention 'BINARY SEARCH' in the read statement even though its already sorted.BINARY search will decrease the search time , thats the purpose of it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;itab_sortd type sorted table with non-unique key MATNR WERKS.&lt;/P&gt;&lt;P&gt;READ TABLE itab_sortd WITH KEY matnr = 'XXXX' &lt;STRONG&gt;BINARY SEARCH&lt;/STRONG&gt;.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Jul 2015 11:57:07 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-on-sorted-table-and-binary-search/m-p/11142582#M1906287</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2015-07-07T11:57:07Z</dc:date>
    </item>
    <item>
      <title>Re: Loop on sorted table and binary search</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-on-sorted-table-and-binary-search/m-p/11142583#M1906288</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;sorry, but although this has been posted ages ago, I must disagree on 3):&lt;/P&gt;&lt;P&gt;When itab_sortd is declared as SORTED TABLE any access (LOOP, READ,...) will be a binary as soon as an initial part of a key is used.&lt;/P&gt;&lt;P&gt;READ TABLE itab_sortd WITH TABLE KEY matnr = 'XXXX' &lt;/P&gt;&lt;P&gt;READ TABLE itab_sortd WITH KEY matnr = 'XXXX' &lt;/P&gt;&lt;P&gt;will both be executed as binary search, because matnr is the first field of primary key. (same applies for secondary keys...)&lt;/P&gt;&lt;P&gt;It is ONLY the STANDARD TABLE that needs explicit addition BINARY SEARCH.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2020 10:30:32 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-on-sorted-table-and-binary-search/m-p/11142583#M1906288</guid>
      <dc:creator>uba_heraeus</dc:creator>
      <dc:date>2020-01-07T10:30:32Z</dc:date>
    </item>
    <item>
      <title>Re: Loop on sorted table and binary search</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/loop-on-sorted-table-and-binary-search/m-p/11142584#M1906289</link>
      <description>&lt;P&gt;&lt;SPAN class="mention-scrubbed"&gt;uba_heraeus&lt;/SPAN&gt; You know, it's not so important to reply to old answers of &lt;A href="https://xkcd.com/386/"&gt;someone is wrong on the internet&lt;/A&gt; &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2020 14:14:47 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/loop-on-sorted-table-and-binary-search/m-p/11142584#M1906289</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2020-01-07T14:14:47Z</dc:date>
    </item>
  </channel>
</rss>

