CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2
  • Faltering Windows support
  • Internet Explorer 8 Click Clever Click Safe
  • Release Candidate 2 for ASP.NET MVC 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > General Discussion > Algorithms & Data Structures
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Algorithms & Data Structures Discuss algorithms & data structures. Topics are not specific to any programming language.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old September 10th, 2009, 12:09 PM
    FeralDruidonWoW's Avatar
    FeralDruidonWoW FeralDruidonWoW is offline
    Member
     
    Join Date: Mar 2009
    Posts: 42
    FeralDruidonWoW is an unknown quantity at this point (<10)
    Runtime question

    I have this problem:

    Consider programs A and B that have been analyzed and found to have worst-case running times no greater than 2n2 and 100nlogn respectively.

    in order to find out if A runs faster than B for certain values should i look for whichever runtime produces the smallest number?

    ex: n = 1000
    2n2 = 2000000
    100nlogn = 30000 so program b runs faster. Is this the correct way to do this problem?
    Reply With Quote
      #2    
    Old September 10th, 2009, 03:34 PM
    Zachm's Avatar
    Zachm Zachm is offline
    Member +
     
    Join Date: Oct 2006
    Posts: 519
    Zachm  is a jewel in the rough (300+) Zachm  is a jewel in the rough (300+) Zachm  is a jewel in the rough (300+) Zachm  is a jewel in the rough (300+)
    Re: Runtime question

    Quote:
    Originally Posted by FeralDruidonWoW View Post
    in order to find out if A runs faster than B for certain values should i look for whichever runtime produces the smallest number?
    In order to find out if A runs faster than B for certain values you should run A and B with those values as input and compare the runtime of each program.

    Worst case merely implies that there exists some input x for which
    A runs 2n^2 steps on, and there exists some input y for which B runs 100 n log n steps on, but it may very well be possible that for other inputs, A and B will have a much shorter run time.

    Consider this program(pseudo-code):
    Code:
    DoSomething(  integer array[n] )
        integer sum <-- 0
        If array[1] = 1 then 
              For i <-- 1 to n, do
                 sum <-- sum + array[i]
              End For
        End If
    End
    Clearly if array[0] is equal to anything but 1, the program runs in O(1) time, but it's worst case is about n steps (O(n) steps) - this will happen if array[1] is equal to 1.

    Regards,
    Zachm
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > General Discussion > Algorithms & Data Structures


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 11:20 AM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.