Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    1
    ReputationRep:
    Hi,

    I'm revising for A2 computing exam and found this question.

    Let a=30, i=1
    While i<n
    For j=i+1 to n
    If [(j*10)/2] < m then m = [(j*10)/2]
    i = i + 1
    Return m

    For an input of 5, how many steps will the algorithm take? How many for the general case? What complexity class?

    I'm good at reading algorithms, just not good at complexity.

    Would greatly appreciate it if someone could answer my question.

    Thanks
    • PS Helper
    • Study Helper
    Offline

    13
    ReputationRep:
    PS Helper
    Study Helper
    (Original post by exam2k10)
    Hi,

    I'm revising for A2 computing exam and found this question.

    Let a=30, i=1
    While i<n
    For j=i+1 to n
    If [(j*10)/2] < m then m = [(j*10)/2]
    i = i + 1
    Return m

    For an input of 5, how many steps will the algorithm take? How many for the general case? What complexity class?

    I'm good at reading algorithms, just not good at complexity.

    Would greatly appreciate it if someone could answer my question.

    Thanks
    OK, what is the nature of the loop? By which I mean, how many times do you run through on each value of i, and how many i's are there? (If you've done any functional programming, it can be easier to see the complexity; I can give a demonstration in Mathematica for why, if you like.)
    You can see that the main body of the loop doesn't really matter because it doesn't alter the values of i or n or j, so the loop goes through the same number of steps if you remove the line starting with If; that might make it easier to see.
    Offline

    3
    ReputationRep:
    (Original post by exam2k10)
    Hi,

    I'm revising for A2 computing exam and found this question.

    Let a=30, i=1
    While i<n
    For j=i+1 to n
    If [(j*10)/2] < m then m = [(j*10)/2]
    i = i + 1
    Return m

    For an input of 5, how many steps will the algorithm take? How many for the general case? What complexity class?

    I'm good at reading algorithms, just not good at complexity.

    Would greatly appreciate it if someone could answer my question.

    Thanks
    For this type of algorithms the complexity with Big O notation is O(n^2)
    For the steps for input n
    Calculate the steps for i=1
    -n-1 comparisons
    -average (n-1)/2 value assignment
    Sum: 3/2(n-1)
    For i=2
    sum: 3/2(n-2)
    and so on
    Total:
    3/2((n-1)+(n-2)+....+1) +n +3 (the term of n is the i=i+1 assignment and 3 is for initialization and return)
    The first part is sum of an arithmetic sequence
    as it known 1+2+ ..... n=n(n+1)/2
    So
    3/2(n-1)n/2+n+3=3/4n^2+1/4n+3<2n^2=O(n^2)
    The steps for n=5
    3/2*4*5/2+6+3=15+9=24
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Brussels sprouts
    Useful resources

    Make your revision easier

    Maths

    Maths Forum posting guidelines

    Not sure where to post? Read the updated guidelines here

    Equations

    How to use LaTex

    Writing equations the easy way

    Student revising

    Study habits of A* students

    Top tips from students who have already aced their exams

    Study Planner

    Create your own Study Planner

    Never miss a deadline again

    Polling station sign

    Thinking about a maths degree?

    Chat with other maths applicants

    Can you help? Study help unanswered threads

    Groups associated with this forum:

    View associated groups
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.