You are Here: Home >< Maths

# Algorithm complexity Watch

1. 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
2. (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.
3. (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

TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: April 13, 2013
Today on TSR

### Last-minute PS help

100s of personal statements examples here

### More pressure for kinky sex?

Discussions on TSR

• Latest
• ## 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
Useful resources

### Maths Forum posting guidelines

Not sure where to post? Read the updated guidelines here

### How to use LaTex

Writing equations the easy way

### Study habits of A* students

Top tips from students who have already aced their exams

## Groups associated with this forum:

View associated groups
Discussions on TSR

• Latest
• ## 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

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