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 problem

former_member925838
Participant
0 Kudos

Dear Experts,

I am having trouble in reading internal table through binary search.

This is my code :

Case Ist:

      SORT it_mseg by mblnr mjahr.
       SORT it_mkpf by mblnr mjahr.

      

       LOOP at it_mseg INTO wa_mseg.
         id1 = sy-tabix.
         READ TABLE it_mkpf INTO wa_mkpf with KEY mblnr = wa_mseg-mblnr mjahr = wa_mseg-mjahr BINARY SEARCH.
         if sy-subrc = 0.
           wa_mseg-budat = wa_mkpf-budat.
           wa_mseg-bldat = wa_mkpf-bldat.
           MODIFY it_mseg FROM wa_mseg INDEX id1.
         ENDIF.
       ENDLOOP.

In above case binary search is working fine.

Case 2nd :

sort it_mseg by matnr DESCENDING.
   it_mseg2[] = it_mseg[].
   SORT it_mseg2 STABLE by matnr DESCENDING budat DESCENDING.

   LOOP at it_final INTO wa_final.
     CLEAR rpu.
     rpu = wa_final-rpu.
    
READ TABLE it_mseg INTO wa_mseg with KEY matnr = wa_final-matnr BINARY SEARCH. " In this case only the material with index 1 is read in     all other cases sy-subrc become 4.

i am not getting why it is doing this as in 1st case it is executing properly.

    if sy-subrc = 0.
     LOOP at it_mseg2 INTO wa_mseg2 FROM sy-tabix.
     if wa_final-MBWBEST gt 0.
     MOVE-CORRESPONDING wa_final to wa_final3.
     if wa_final-MBWBEST ge wa_mseg2-menge.
       wa_final3-mblnr = wa_mseg2-mblnr.
       wa_final3-budat = wa_mseg2-budat.
       wa_final3-MBWBEST = wa_mseg2-menge.

       wa_final-MBWBEST = wa_final-MBWBEST - wa_final3-MBWBEST.

        IF wa_final3-budat IS NOT INITIAL.
       wa_final3-cnt = s_date - wa_final3-budat"LETZTABG.
       ENDIF.

       wa_final3-rpu = rpu * wa_final3-MBWBEST.
       APPEND wa_final3 to it_final3.


     ELSe. "wa_final-MBWBEST lt wa_mseg2-menge_i.
       wa_final3-mblnr = wa_mseg2-mblnr.
       wa_final3-budat = wa_mseg2-budat.
       wa_final3-MBWBEST = wa_final-MBWBEST.

        IF wa_final3-budat IS NOT INITIAL.
       wa_final3-cnt = s_date - wa_final3-budat"LETZTABG.
       ENDIF.
        wa_final3-rpu = rpu * wa_final3-MBWBEST.
       APPEND wa_final3 to it_final3.
       wa_final-MBWBEST = 0.
     ENdif.

   ENDIF.
   at END OF matnr.
     exit.
   ENDAT.
     ENDLOOP.
   ENDIF.
   ENDLOOP.

Please help

Thanks,

Amar


1 ACCEPTED SOLUTION

Former Member

Hi Amar,

In Case # 1 :  You use 

       SORT it_mseg by mblnr mjahr.         

      SORT it_mkpf by mblnr mjahr.


                                                                               " without using ASCENDING / DESCENDING option

                                                                                so SAP by default SORT in ASCENDING then ur

                                                                                 BINARY SEARCH WORKING F9 .


      

                             READ TABLE it_mkpf INTO wa_mkpf with KEY mblnr = wa_mseg-mblnr

                                                       mjahr =wa_mseg-mjahr BINARY SEARCH.

In Case # 2 :

                     

sort it_mseg by matnr DESCENDING        

                                                                            "  Using Sort in Descending order this is a main reason ur

                                                                                BINARY SEARCH Not Working .


READ TABLE it_mseg INTO wa_mseg with KEY matnr = wa_final-matnr BINARY SEARCH.

          

Please check

  • 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.

  • 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. If the specified search key is or inclucdes an starting field of the table key, the addition BINARY SEARCH can be specified for sorted tables, but has no effect.

  • 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.

Regard's

Smruti

3 REPLIES 3

Former Member

Hi Amar,

In Case # 1 :  You use 

       SORT it_mseg by mblnr mjahr.         

      SORT it_mkpf by mblnr mjahr.


                                                                               " without using ASCENDING / DESCENDING option

                                                                                so SAP by default SORT in ASCENDING then ur

                                                                                 BINARY SEARCH WORKING F9 .


      

                             READ TABLE it_mkpf INTO wa_mkpf with KEY mblnr = wa_mseg-mblnr

                                                       mjahr =wa_mseg-mjahr BINARY SEARCH.

In Case # 2 :

                     

sort it_mseg by matnr DESCENDING        

                                                                            "  Using Sort in Descending order this is a main reason ur

                                                                                BINARY SEARCH Not Working .


READ TABLE it_mseg INTO wa_mseg with KEY matnr = wa_final-matnr BINARY SEARCH.

          

Please check

  • 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.

  • 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. If the specified search key is or inclucdes an starting field of the table key, the addition BINARY SEARCH can be specified for sorted tables, but has no effect.

  • 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.

Regard's

Smruti

0 Kudos

Thanks Smruti,

it was very silly mistake.

thanks for the help.

Amar

sudipDas
Explorer
0 Kudos

Hi

Do not put DESCENDING key word in sorting it_mseg.

Regards

Sudip