Results 1 to 4 of 4

Thread: Time complexity of algorithms

  1. #1

    Time complexity of algorithms

    This is a little quiz...

    What's the uniform time complexity of the following algorithms?

    1) Very simple
    [pascal]
    function DoSomething1(S: string): string;
    begin
    Result := S + 'Hello World';
    end;
    [/pascal]
    a) O(1)
    b) O(n)
    c) O(n<sup>2</sup>)


    2) Still quite simple
    [pascal]
    function DoSomething2(S: string): string;
    var
    I: Integer;
    begin
    Result := S;
    for I := 1 to Length(S) do
    Result := Result + 'Hello World';
    end;
    [/pascal]
    a) O(1)
    b) O(n)
    c) O(n<sup>2</sup>)


    3) A bit harder
    [pascal]
    function DoSomething3(X: Integer): Integer;
    var
    I: Integer;
    begin
    I := 1;
    while I * I <= X do
    I := I + 1;
    Result := I - 1;
    end;
    [/pascal]
    a) O(1)
    b) O(n)
    c) O(n<sup>2</sup>)

    4) Hard
    [pascal]
    function DoSomething4(N: Integer): Integer;
    var
    Sum, I, J, K: Integer;
    begin
    Sum := 0;
    for I := 1 to N do
    for J := 1 to (N div I) do
    for J := 1 to N do
    Sum := Sum + K;
    Result := Sum;
    end;
    [/pascal]
    a) O(n<sup>3</sup>)
    b) O(n<sup>2</sup> log(n))
    c) O(n log<sup>2</sup> (n))
    d) O(n log(n))
    Ask me about the xcess game development kit

  2. #2
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Time complexity of algorithms

    My answers

    1 - a
    2 - b
    3 - c
    4 - b
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  3. #3

    Time complexity of algorithms

    Almost right.
    3) takes sqrt(n) steps to complete and that's in O(n)

    For everybody who hasn't heard of O-Notation before...
    From a mathematical point of view, it's a way to describe how something develops or grows.
    When programming, O-Notation can be used to describe both the time- and space complexity of an algorithm.
    Of course you could check how long an algorithm takes to complete with a stop watch (or by implementing something similar into your code), but that would give you a result that depends on the speed of your computer.
    With O-Notation, the only "factor" in the equation is the length of the input value.

    Algorithm 1) (see above) completes in one step and that step is independent from the input length. That's why it's in O(1) which is also called "constant time complexity". If it completed in 100 steps, it would also be in O(1) if those steps were independent from the length of the input word.

    In algorithm 2) there is a for-loop that goes through the entire length of the input string. If the length of that string is "n", then the time complexity is in O(n). This is also called "linear time complexity".

    In algorithm 2) the input length is the value of "x". Since the program calculates the integer-sqrt of the number x, it takes sqrt(x) steps to complete. The execution time depends on the length of x and even though sqrt(x) is < x, it's still in O(n) (because you don't normally write O(sqrt(n)).

    In algorithm 3) you have two for-loops from 1 to N that are nested, so that already gives you n<sup>2</sup>. The second for loop from 2 to (N div I) takes <= log(n) steps to complete and since all three loops are nested, you get O(n<sup>2</sup>log(n)).

    O-Notation is very useful for comparing algorithms, for example when comparing sorting algorithms.
    Thought I'd share this with you... :lol:
    Ask me about the xcess game development kit

  4. #4
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Time complexity of algorithms

    Last time I found this usefull was at university 12 years ago

    Since then its been - which is fastest - if neccessary test two ways on the same machine and decide
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •