Linear search and binary search

Hi
can any one tell me what is linear and binary search in detail.
and what is the difference between them .
which one is useful in coding.
Thanks&Regards,
S.GangiReddy.

hi,
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.
READ TABLE <itab> WITH KEY <k1> = <f1>... <kn> = <fn> <result>  BINARY SEARCH.
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.
REPORT demo_int_tables_read_index_bin.
DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.
DATA itab LIKE STANDARD TABLE OF line.
DO 4 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  APPEND line TO itab.
ENDDO.
SORT itab BY col2.
READ TABLE itab WITH KEY col2 = 16 INTO line BINARY SEARCH.
WRITE: 'SY-SUBRC =', sy-subrc.
The output is:
SY-SUBRC =    0
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.
Linear search use sequential search means each and every reord will be searched to find. so it is slow.
Binary search uses logrim for searching. Itab MUST be sorted on KEY fields fro binary search. so it is very fast.
The search takes place as follows for the individual table types :
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. 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.
Binary search must be preffered over linear sarch.
Hope this is helpful, Do reward.

Similar Messages

  • Linear and Binary Searching of Parallel Arrays

    I'm an AP student who used the "Fundamentals of Java" by Lambert/Osborne 3rd Edition. This text book has code that doesn't match anything else I've found in other how-to's, guides, or teach yourself books. Not even online can I find code that matches up with the format used in this book!
    I've got an assignment that wants me to read in a 4 digit account number of N number of customers, places them in two parallel arrays. Data found in two separate txt files. Create a prompt that ask's for customer's account number then displays account balance. Same program for both linear and binary search methods (2 programs).
    I know the search method and how to read the files with a scanner. It is the body of the program that is stumping me. How to call and search the arrays themselves. Any help would be great.

    First of all, you have posted this question in the wrong place. Please post these kinds of general questions in the New to Java forum.
    Second, if you're in an AP class, don't you have a teacher you can ask?
    But anyway, here's the idea
    For a linear search, you go thru the array element by element, and on each element you call equals(x) to see if that element is equal to what you're searching for (note that primitives use == rather than equals)
    For binary search, note first of all that your data MUST BE COMPARABLE (primative or implement the comparable interface) and MUST BE SORTED.
    Then what you can do if go to the middle of the list, and if what you are searching for is less than that element, go to the middle of the first half of the list (if it's greater, go the the middle of the upper half of the list) and keep breaking the list in half until you've found the element or you know its not there.

  • Problem with removing Search and Advanced Search functionalities in Portal

    Hi,
          As per our client requirement, we have removed search and advanced search functionalities in Masthead and those are working fine. But when ever we restarted the portal server, these search options are reappearing in portal.
              I am not getting why it is happening and every time we restarted the server, we are removing these search functionalities from masthead.
              If anyone knows why it is happening, kindly share with us
    Thanks & Regards
    Pavan

    Hi Santosh,
                         Problem is not solved. Where can i find the Standard tool area iview used in Default frame work page ?
    At present, we disabeled the search and it's working fine. But when we restart the portal, search is again reappearing and we need to disable that once again.
               Is there any sollution for this problem?? Kindly let me know..
    Thanks & Regards
    Pavan
    Edited by: pavanakrishna reddy on Mar 24, 2010 9:44 AM

  • How can I get my home page toshow both web search and ountry search?

    My Home page no longer shows both google web search and uk search it only shows google web search.
    How can I correct this

    Instructions in this article: [[How to set the home page]]

  • Hide Search and Quick Search

    Is it possible to show/hide Search and Quick Search depending on who logs in to UCM?

    Hi Malky,
    I had a similar requirement and i tried to hide the standard search link.I am not able t restrict and its on 11g.
    please find the snippet of code and let me know your sugessions.
    thanks in advance.
    <@dynamichtml std_add_search_links@>
    <$include super.std_add_search_links$>
              // Add search links
              var count = 0;
              if (typeof pneSearchDocProfiles != "undefined" && pneSearchDocProfiles)
                   count = pneSearchDocProfiles.length;
                   if (count > 0)
                        navBuilder.addChildNodeTo('NAVTREE', 'collection', 'id==SEARCH', 'label=='+lc('wwSearch'));
                        for (var i = 0; i < count; i++)
                             navBuilder.addChildNodeTo('SEARCH', 'item', 'id==MY_PERSONAL_SEARCHES_'+i, 'label=='+pneSearchDocProfiles[0],
                                  'url=='+pneSearchDocProfiles[i][1]);
              if (count > 0)
                   // Add the standard search link to the drop down menu link. Commented to disable
                   if(userIsAdmin)
                        navBuilder.addChildNodeTo('SEARCH', 'item', 'id==MY_PERSONAL_SEARCHES_'+count, 'label=='+lc('wwStandardSearch'),
                        'url=='+httpCgiPath+'?IdcService=GET_DOC_PAGE&Action=GetTemplatePage&Page=STANDARD_QUERY_PAGE');
              else
                   // Just add the link to the top menu.
                   navBuilder.addChildNodeTo('NAVTREE', 'item', 'id==SEARCH', 'label=='+lc('wwSearch'),
                        'url=='+httpCgiPath+'?IdcService=GET_DOC_PAGE&Action=GetTemplatePage&Page=STANDARD_QUERY_PAGE');
    <@end@>

  • LSO - Search and Extended Search are not working

    Hi ,
    From the learning portal we click on Search and Extended Search , nothing is diplay by the system . We have course matching the description . Also in the back office : LSO_TRAININGTYPE_SEARCH_C seems to be OK .
    Any help will be welcome, should work out of the box .
    Thank you.

    Hi Schutz
    Were you able to resolve this? If so could you please provide the solution. We are also facing the same kind of problem. It works in QA but not in Production.
    Thanks, Raj

  • Read table and binary search

    Hi all,
    I have a simple query regarding read int_tab with binary search.
    Why reading  internal table with binary search fails if it is sorted in descending order table must be sorted in ascending order?
    I check fo the algorithm of binary search, it does not talk about sort order. As far as my understanding goes binary search only require sorted table but while reading table in SAP it has to be sorted in ascending order!!

    By default binary search assumes that the sort order is ASCENDING.
    If you sort the list in descending and then try to binary search your quires will fail. Look at an example:
    Let the descending order internal table as:
    Field1      Field2
    Sam        50000
    John       34786
    Boob      54321
    Alice       12345
    When you do binary search with key = 'Sam' then it will directly go to 2nd and 3rd records for comparision. The binary search algorithm compares 'Sam' with 'John' and it concludes that 'Sam' is greater than 'John' and it will continue to look downward into the internal table. And when it reaches the end of the internal table then the value of SY-TABIX = 5 and SY-SUBRC = 8 (Key is greater than the all).
    And if you do binary search with key = 'Alice' then the binary search algorithm compares 'Alice' with 'John' and it concludes that 'Alice' is lower than 'John' and it will continue to look upward into the internal table. And when it reaches above the first record in internal table then the value of SY-TABIX = 1 and SY-SUBRC = 4 (points to the next largest entry).
    The only correct result you will get is when you execute statement with key='John' (In this particular case) . SY-TABIX = 2 and SY-SUBRC = 0. I think you got this binary search algorithm.

  • Arrays,bubblesort, and binary search

    Hi I need help I have been working on this homework assignment for the past week and I can't seem to get it so if anyone can help me to figure out what is wrong I would reall grateful. Thanks ahead of time for the help.
    Here is what is required for the assignment and also the errors I am getting.
    Thanks!
    Write a program that sorts an integer array in ascending order and checks whether an integer entered by user is in the array or not. Please follow the following steps to complete the assignment:
    1. Declare and create a one-dimensional array consisting of 20 integers.
    2. Read 20 integers from the user to initialize the array. Use input dialog box and repetition statement.
    3. Build an output string containing the content of the array.
    4. Sort the array in ascending order using bubbleSort( ) and swap( ) methods. Then, append the content of the sorted array to the output string.
    5. Read an integer search key from the user;
    6. Use binarySearch( ) method to check whether the search key is in the array or not. Then, append the search result to the output string.
    7. Display the output string in a message box.
    Here is my code
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class SortSearch {
    public static void main (String args[])
    String input, ouput;
    int key;
    int index;
    int[] array=new int [20];
    input=JOptionPane.showInputDialog("Enter 20 Numbers");
    for(int counter=0; counter<array.length; counter++)
    output+=counter+"\t"+array[counter]+"\n";
    array(counter)=Integer.parseInt(input);
    JTextArea inputArea=new JTextArea();
    outputArea.setText(output);
    public void bubblesort(int array2[] )
    for(int pass=1; pass<array2.length; pass++){
    for(int element=0; element<array2.length-1; element++){
    if(array2[element]>array2[element+1])
    swap(array2, element, element+1);
    public void swap(int array3[], int first, int second)
    int hold;
    hold=array3[first];
    array3[first]=array3[second];
    array3[second]=hold;
    public void actionPerformed(ActionEvent actionEvent)
    String searchKey=actionEvent.getActionCommand();
    int element=binarySearch(array, Integer.parseInt(searchKey) );
    if(element!=-1)
    output.setText("Found value in element " + element);
    else
    output.setText("Value not found ");
    public int binary search(iny array2[], int key)
    int low=0;
    int high=array2.length-1;
    int middle;
    while(low<=high){
    middle=(low + high)/2;
    buildOutput(array2, low, middle, high);
    if(key==array[middle] )
    return middle;
    else if(key<array[middle] )
    high=middle-1;
    else
    low=middle+1
    return-1
    JOptionPane.showMessageDialog(null, outputArea);
    System.exit(0);
    } //end main
    } //end class
    Here is my errors
    C:\java>javac SortSearch.java
    SortSearch.java:27: illegal start of expression
    public void bubblesort(int array2[] )
    ^
    SortSearch.java:20: cannot resolve symbol
    symbol : variable output
    location: class SortSearch
    output+=counter+"\t"+array[counter]+"\n";
    ^
    SortSearch.java:22: cannot resolve symbol
    symbol : variable counter
    location: class SortSearch
    array(counter)=Integer.parseInt(input);
    ^
    SortSearch.java:25: cannot resolve symbol
    symbol : variable output
    location: class SortSearch
    outputArea.setText(output);
    ^
    SortSearch.java:25: cannot resolve symbol
    symbol : variable outputArea
    location: class SortSearch
    outputArea.setText(output);
    ^
    5 errors

    I am still having problems.
    Here is my code
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class SortSearch {
          public static void main (String args[]){
          String input, output;
          int key;
          int index;
          int[] array=new int [20];
          input=JOptionPane.showInputDialog("Enter 20 Numbers");
          for(int counter=0; counter<array.length; counter++)
            array(counter)=Integer.parseInt(input);
          JTextArea outputArea=new JTextArea();
          outputArea.setText(output);
            public void bubblesort(int array2[] )
              for(int pass=1; pass<array2.length; pass++){
                for(int element=0; element<array2.length-1; element++){
                  if(array2[element]>array2[element+1]) 
                    swap(array2, element, element+1);
                }  //end inner for
              }  //end outer for
            }  //end bubblesort 
            public void swap(int array3[], int first, int second)
              int hold;
              hold=array3[first];
              array3[first]=array3[second];
              array3[second]=hold;
            }  //end swap
            public void actionPerformed(ActionEvent actionEvent)
              String searchKey=actionEvent.getActionCommand();
              int element=binarySearch(array, Integer.parseInt(searchKey) );
              if(element!=-1)
                outputArea.setText("Found value in element " + element);
              else
                outputArea.setText("Value not found ");
            }  //end actionperformed
            JOptionPane.showMessageDialog(null, outputArea,"Comparisons");       
    }  //end classHere is my errors
    C:\java>javac SortSearch.java
    SortSearch.java:57: <identifier> expected
    JOptionPane.showMessageDialog(null, outputArea,"Comparisons");
    ^
    SortSearch.java:57: cannot resolve symbol
    symbol : class showMessageDialog
    location: class javax.swing.JOptionPane
    JOptionPane.showMessageDialog(null, outputArea,"Comparisons");
    ^
    SortSearch.java:19: cannot resolve symbol
    symbol : method array (int)
    location: class SortSearch
    array(counter)=Integer.parseInt(input);
    ^
    SortSearch.java:49: cannot resolve symbol
    symbol : variable array
    location: class SortSearch
    int element=binarySearch(array, Integer.parseInt(searchKey) );
    ^
    SortSearch.java:52: cannot resolve symbol
    symbol : variable outputArea
    location: class SortSearch
    outputArea.setText("Found value in element " + element);
    ^
    SortSearch.java:54: cannot resolve symbol
    symbol : variable outputArea
    location: class SortSearch
    outputArea.setText("Value not found ");
    ^
    6 errors
    Thanks ahead of time I still don't understand the stuff so I sometime don't understand what my errors are telling me so that is why I ask for your help so that maybe it will help make sense.

  • Searching a binary search tree  written to a file

    hello
    I have a binary search tree which has 10 levels. Now I want to search a node
    after writing all the nodes to a file.
    Can anyone help me in doing this. A sample code would be great ...
    Thanks
    K

    You suggest that I do away with the inner class
    then?Absolutely. In fact I strongly suggest this. You are learning how to code and need to do things cleanly and in small steps. That means first creating your Node class and making sure it works. Then creating your Tree class, and making sure it works. In fact I would load the Strings into the Tree class first directly and testing things before even thinking about reading to and from files. Only then should you implement the file input and output steps.
    The key here is that you don't go on to the next step until you're reasonably sure that your current code works. Remember, it's MUCH easier to code than to debug.

  • Document search and knowledge search

    Hi All,
    I have seen two work centres on interaction center:
    document search and knowledge serach.
    what is the difference between the two.
    how can we configure the same.
    please provide some docs.
    Thanks,
    Priya

    Hello Priya,
    Knowledge Search is used to search for solutions to problems using the Software Agent Framework (SAF) to search against the Solution Database (SDB) or other knowledge repositories that you configure (such as Case Management or Service Tickets).
    The Document Search is for searching for related documents that might be linked to the confirmed account using Content Management.
    You can find detailed information and configuration instructions in the online help.
    Warm regards,
    John

  • Help, my Safari browser has been acting super strange.  It keeps changing my default browser from Google to Only Search, and my search bar will only search on Yahoo no matter what I do.  On top of that I get MacKeeper popups every time I click a link

    Help, my Safari browser has been acting super strange lately!  It keeps changing my default browser automatically from Google to Only Search (if you use the search engine Only Search it takes you to Yahoo.com).  My search bar will only search on Yahoo no matter what I do to make it Google.  On top of that I get MacKeeper popups every time I click on a link.  What is worn with my computer and how do I fix it?!

    There is no need to download anything to solve this problem. You may have installed a variant of the "VSearch" ad-injection malware.
    Triple-click the line below on this page to select it, then copy the text to the Clipboard by pressing the key combination  command-C:
    /Library/LaunchDaemons
    In the Finder, select
              Go ▹ Go to Folder...
    from the menu bar and paste into the box that opens by pressing command-V. You won't see what you pasted because a line break is included. Press return.
    A folder named "LaunchDaemons" may open. Look inside it for a file with a name of the form
              com.something.daemon.plist
    Here something is a variable word, which can be different in each case. It could be "cloud," "dot," "highway," "submarine," "trusteddownloads," or pretty much anything else.
    There may also be a file named
               com.something.helper.plist
    in the same folder.
    If you find files with names that fit the above description, post what you have for "something."

  • Binary Search and Sequence Search in Archives

    My friends, I need of a example of these alghoritms . Can you help me abouth this?
    thanks

    You are not going to get far in the programming business if you do not know how to search the Internet for information. It's true, it isn't easy if you don't know English well, but that is not going to change. You will not be able to do your work if you constantly have to be asking other people to search the web for you.

  • I use both add to search and organize search, FF34 has no search box, how do I add one?

    I tend to use Site Search, if available, instead of bookmarks, I use "add to searchbar" to make the proper xml file, and then I use organize search to group by category.. (373 user search xml files currently) as soon as FireFox upgraded to 34, I see a blank space on the toolbar instead of the searchbox.. I need the searchbox to get at all my saved search providers. BTW, "Search Using" works normal, providing my expected menu of organized providers. Same problem/symptoms, both Windows 7 systems I use.

    If you want to restore the previously used standalone window to manage search engines then you can toggle this pref to false with a middle-click on the about:config page.
    about:config page: browser.search.showOneOffButtons = false
    You need to close and restart Firefox to make this work properly.
    In the address bar, type '''about:preferences#search<enter>'''
    If you miss being able to switch search engines without going
    crazy with menus, try this out;
    '''[https://addons.mozilla.org/en-US/firefox/addon/context-search/ Context Search]''' {web link}
    Expands the context menu's 'Search for' item into a list of
    installed search engines, allowing you to choose the engine
    you want to use for each search.

  • Please give feedback on some searches (and the search experience generally).

    Please read
    this post on my blog and give feedback on your search experience.

    Not bad for a beginner.
    You might want to consider working on your image optimization
    - the projects
    page is >300K in weight, and it should be no more than
    100K. You should
    also NOT name your files/folders with names containing spaces
    (or any other
    punctuation, other than hyphen and underscore).
    You might also consider these validation results -
    http://validator.w3.org/check?verbose=1&uri=http%3A%2F%2Fwww.tallis-surveyors.co.uk%2FProj ects.html
    Pages with invalid code/usages lead to pages that render
    unreliably in
    various browsers.
    Murray --- ICQ 71997575
    Adobe Community Expert
    (If you *MUST* email me, don't LAUGH when you do so!)
    ==================
    http://www.projectseven.com/go
    - DW FAQs, Tutorials & Resources
    http://www.dwfaq.com - DW FAQs,
    Tutorials & Resources
    ==================
    "datsomset" <[email protected]> wrote in
    message
    news:fr5tlh$r7e$[email protected]..
    > Could anyone please give me some feedback on this site
    (below)? I am not a
    > web
    > builder, just a business person having fun building a
    site for my
    > business. I
    > know I need to reduce the size of the images, can you
    suggest any other
    > improvements?
    >
    > I would also like to build an email template using the
    same css layout. I
    > tried this, but the css design did not show up in the
    inbox of the pc I
    > sent it
    > too. Do I need to do an inline css?
    >
    > Thanks in advance for your help, I always find great and
    friendly
    > assistance
    > on this forum :)
    >
    >
    http://www.tallis-surveyors.co.uk
    >

  • Default Sort order for Search and Advanced Search

    Does anybody know of any easy way to default search to show results by "last modified date" ?
    Much thanks,
    ~Kevin.

    You might try logging in as the guest user and editing the guest user preferences / search preferences as described by in the earlier posting. Also you can configure search by passing parameters in the URL - should be info about that in here somewhere...

Maybe you are looking for

  • Archived log files not registered in the Database

    I have Widows Server 2008 R2 I have Oracle 11g R2 I configured primary and standby database in 2 physical servers , please find below the verification: I am using DG Broker Renetly I did failover from primary to standby database Then I did REINSTATE

  • I have a question about iphone on 3G.

    I've heard that Apple may publish the new Iphone next year, and it will support 3G, since the old version didn't support 3G, I'd ask whether the old version could be updated to support 3G? That is to say, is the 3G function existed in the old phone?

  • Cyrillic (Russian) Text Problems

    Hi, I've been having a problem with a Russian site for awhile. I have the encoding for all pages set to UTF-8. I'll make my page with Russian characters and it will upload to the live site just fine and I'll have no issues with it for a few months. T

  • Delayed to stablish a call from jabber to jabber

    Good day I am presenting a problem: when I call from jabber device ( jabber for windows, ipad,) other device jabber (jabber for windows, ipad), the call sets after 8 sec, witch ip comunicator the call is immediately established. Is normal this in jab

  • Satellite P35-S6112 - No sound after Windows 7 installation

    I installed Windows 7 on this Satellite P35-S6112 with a Realtek AC97 sound card in it. The card and drivers are working but there is no volume. I can hear sound with a headset very faint. I have uninstalled, reinstalled, attempted update but I have