How is Big O of selection sort calculated mathematically? - algorithm

Can someone please explain in plain English how to calculate it?
I know that you have to visit n+2+(n-1)+2+...+2+2 elements, but how do you get to 1/2n^2 + 5/2n - 3? Thanks!

n + 2 + (n - 1) + 2 + ... + 2 + 2 is equal to (n + 2) + (n + 1) + ... + 4. It's an arithmetic progression and its sum is calculated as (n + 2 + 4) * (n + 2 - 4 + 1) / 2. It's equal to (n + 6) * (n - 1) / 2 and finally 1/2 * n^2 + 5/2 * n - 3.
f(n) = O(g(n)) means there exists such constant C that f(n) <= C * g(n) for all sufficiently large n. If n is considered as natural number then 1/2 * n^2 + 5/2 * n - 3 = O(n^2) with C = 3/2 for example.

Related

Determine the big O running time

I am struggling with this question would like some help , thank you.
Determine the big O running time of the method myMethod() by counting the
approximate number of operations it performs. Show all details of your answer.
Note: the symbol % represent a modulus operator, that is, the remainder of a
number divided by another number.
๐‘ ๐‘ก๐‘Ž๐‘ก๐‘–๐‘ ๐‘–๐‘›๐‘ก ๐‘š๐‘ฆ๐‘€๐‘’๐‘กโ„Ž๐‘œ๐‘‘(๐ด[], ๐‘›) {
๐‘๐‘œ๐‘ข๐‘›๐‘ก โ† 0
๐‘“๐‘œ๐‘Ÿ ๐‘– โ† 0 ๐‘ก๐‘œ ๐‘› โˆ’ 1 {
๐‘“๐‘œ๐‘Ÿ ๐‘— โ† ๐‘– ๐‘ก๐‘œ ๐‘› โˆ’ 1 {
๐‘๐‘œ๐‘ข๐‘›๐‘ก โ† ๐‘๐‘œ๐‘ข๐‘›๐‘ก+ ๐ด[๐‘—]
๐‘˜ โ† 1
๐‘คโ„Ž๐‘–๐‘™๐‘’ (๐‘˜ < ๐‘› + 2) {
๐‘–๐‘“ (๐‘—%2 == 0) {
๐‘๐‘œ๐‘ข๐‘›๐‘ก = ๐‘๐‘œ๐‘ข๐‘›๐‘ก + 1
}
๐‘˜ + +
}
}
}
๐‘Ÿ๐‘’๐‘ก๐‘ข๐‘Ÿ๐‘› ๐‘๐‘œ๐‘ข๐‘›๐‘ก
}
The outer for loop with the variable i runs n times.
The inner for loop with the variable j runs n - i times for each iteration of the outer loop. This would make the inner loop run n + n-1 + n-2 +...+ 1 times in aggregation which is the equivalent of n * (n+1) / 2.
The while loop inside the inner for loop runs n + 1 times for each iteration of the inner for loop.
This makes the while loop to run (n * (n+1) / 2) * (n + 2). This produces (n^2 + n) / 2 * (n + 2) = (n^2 + n) * (n + 2) / 2 = (n^3 + 2n^2 + n^2 + 2n) / 2 = (n^3 + 3n^2 + 2n) / 2).
Dropping lower degrees of n and constants we get O(n^3).
You could have also argued that n + n-1 + ... + 1 is O(n^2) times a linear operation becomes O(n^3). Which would have been more intuitive and faster.

How does the time complexity of the bubble sort algorithm result in O(n^2) regarding the way you calculate it?

I understand that why bubble sort is O(n^2).
However in many explanations I see something like this:
(n-1) + (n-2) + (n-3) + ..... + 3 + 2 + 1
Sum = n(n-1)/2
How do you calcuate Sum from this part:
(n-1) + (n-2) + (n-3) + ..... + 3 + 2 + 1
Can anyone help?
Here's the trick:
If n is even:
n + (n-1) + (n-2) + โ€ฆ + 3 + 2 + 1
= [n + 1] + [(n-1) + 2] + [(n-2) + 3] + โ€ฆ + [(n - (n/2 - 1)) + n/2]
= (n + 1) + (n + 1) + (n + 1) + โ€ฆ + (n + 1)
= n(n+1)/2
If n is odd:
n + (n-1) + (n-2) + โ€ฆ + 3 + 2 + 1
= [n + 1] + [(n-1) + 2] + [(n-2) + 3] + โ€ฆ + [(n - (n-1)/2 + 1) + (n-1)/2] + (n-1)/2 + 1
= (n+1) + (n+1) + (n+1) + โ€ฆ + (n+1) + (n-1)/2 + 1
= (n+1)(n-1)/2 + (n-1)/2 + 1
= (n^2 - 1 + n - 1 + 2)/2
= (n^2 + n)/2
= n(n+1)/2
For your case, since you're counting up to n-1 rather than n, replace n with (n-1) in this formula, and simplify:
x(x+1)/2, x = (n-1)
=> (n-1)((n-1)+1)/2
= (n-1)(n)/2
= n(n-1)/2
The simplest "proof" to understand without deriving the equation is to imagine the complexity as area:
so if we have sequence:
n+(n-1)+(n-2)...
we can create a shape from it... let consider n=5:
n 5 *****
n-1 4 ****
n-2 3 ***
n-3 2 **
n-4 1 *
Now when you look at the starts they form a right angle triangle with 2 equal length sides ... that is half of n x n square so the area is:
area = ~ n.n / 2 = (n^2)/2
In complexities the constants are meaningless so the complexity would be:
O(n^2)

How to solve T(n)=T(n-1)+ (n-1) by Iteration Method?

Please can anyone help me with this:
Solve using iteration Method T (n) = T (n - 1) + (n - 1)
And prove that T (n) โˆˆฮ˜ (nยฒ)
Please, if you can explain step by step I would be grateful.
I solved an easy way :
T (n) = T (n - 1) + (n - 1)-----------(1)
//now submit T(n-1)=t(n)
T(n-1)=T((n-1)-1)+((n-1)-1)
T(n-1)=T(n-2)+n-2---------------(2)
now submit (2) in (1) you will get
i.e T(n)=[T(n-2)+n-2]+(n-1)
T(n)=T(n-2)+2n-3 //simplified--------------(3)
now, T(n-2)=t(n)
T(n-2)=T((n-2)-2)+[2(n-2)-3]
T(n-2)=T(n-4)+2n-7---------------(4)
now submit (4) in (2) you will get
i.e T(n)=[T(n-4)+2n-7]+(2n-3)
T(n)=T(n-4)+4n-10 //simplified
............
T(n)=T(n-k)+kn-10
now, assume k=n-1
T(n)=T(n-(n-1))+(n-1)n-10
T(n)=T(1)+n^2-n-10
According to the complexity 10 is constant
So , Finally O(n^2)
T(n) = T(n - 1) + (n - 1)
= (T(n - 2) + (n - 2)) + (n - 1)
= (T(n - 3) + (n - 3)) + (n - 2) + (n - 1)
= ...
= T(0) + 1 + 2 + ... + (n - 3) + (n - 2) + (n - 1)
= C + n * (n - 1) / 2
= O(n2)
Hence for sufficient large n, we have:
n * (n - 1) / 3 โ‰ค T(n) โ‰ค n2
Therefore we have T(n) = ฮฉ(nยฒ) and T(n) = O(nยฒ), thus T(n) = ฮ˜ (nยฒ).
T(n)-T(n-1) = n-1
T(n-1)-T(n-2) = n-2
By substraction
T(n)-2T(n-1)+T(n-2) = 1
T(n-1)-2T(n-2)+T(n-3) = 1
Again, by substitution
T(n)-3T(n-1)+3T(n-2)-T(n-3) = 0
Characteristic equation of the recursion is
x^3-3x^2+3x-1 = 0
or
(x-1)^3 = 0.
It has roots x_1,2,3 = 1,
so general solution of the recursion is
T(n) = C_1 1^n + C_2 n 1^n + C_3 n^2 1^n
or
T(n) = C_1 + C_2 n + C_3 n^2.
So,
T(n) = ฮ˜(n^2).

Solving recurrence for T(n-1) + sqrt(n)

I'm hoping that I'm going about this problem the correct way. It asks to solve the recurrence:
T(n) = T(n-1) + sqrt(n)
So far I have researched and been able to get to this point:
T(n) = T(n-2) + (n-1) + sqrt(n)
T(n) = T(n-3) + (n-2) + (n-1) + sqrt(n)
T(n) = T(0) + 1 + 2 + ... + (n-2) + (n-1) + sqrt(n)
I'm having trouble understanding what the pattern may be to solve for 1+2+...+sqrt(n)
You start with unrolling the recursion and you should receive a sum of square roots. The sum of square roots is a generalized harmonic number and yours one can be approximated with:
The second line is already wrong.
If T (n) = T (n - 1) + sqrt (n), then T (n - 1) = T (n - 2) + sqrt (n - 1), therefore
T (n) = T (n - 2) + sqrt (n - 1) + sqrt (n)
T (n) = T (n - 3) + sqrt (n - 2) + sqrt (n - 1) + sqrt (n)
T (n) = T (n - 4) + sqrt (n - 3) + sqrt (n - 2) + sqrt (n - 1) + sqrt (n)
and so on.
The sum of the square roots from 1 to n is about the same as the integral of sqrt (x) from 1 to n.

How to solve: T(n) = T(n - 1) + n

I have the following worked out:
T(n) = T(n - 1) + n = O(n^2)
Now when I work this out I find that the bound is very loose. Have I done something wrong or is it just that way?
You need also a base case for your recurrence relation.
T(1) = c
T(n) = T(n-1) + n
To solve this, you can first guess a solution and then prove it works using induction.
T(n) = (n + 1) * n / 2 + c - 1
First the base case. When n = 1 this gives c as required.
For other n:
T(n)
= (n + 1) * n / 2 + c - 1
= ((n - 1) + 2) * n / 2 + c - 1
= ((n - 1) * n / 2) + (2 * n / 2) + c - 1
= (n * (n - 1) / 2) + c - 1) + (2 * n / 2)
= T(n - 1) + n
So the solution works.
To get the guess in the first place, notice that your recurrence relationship generates the triangular numbers when c = 1:
T(1) = 1:
*
T(2) = 3:
*
**
T(3) = 6:
*
**
***
T(4) = 10:
*
**
***
****
etc..
Intuitively a triangle is roughly half of a square, and in Big-O notation the constants can be ignored so O(n^2) is the expected result.
Think of it this way:
In each "iteration" of the recursion you do O(n) work.
Each iteration has n-1 work to do, until n = base case. (I'm assuming base case is O(n) work)
Therefore, assuming the base case is a constant independant of n, there are O(n) iterations of the recursion.
If you have n iterations of O(n) work each, O(n)*O(n) = O(n^2).
Your analysis is correct. If you'd like more info on this way of solving recursions, look into Recursion Trees. They are very intuitive compared to the other methods.
The solution is pretty easy for this one. You have to unroll the recursion:
T(n) = T(n-1) + n = T(n-2) + (n - 1) + n =
= T(n-3) + (n-2) + (n-1) + n = ... =
= T(0) + 1 + 2 + ... + (n-1) + n
You have arithmetic progression here and the sum is 1/2*n*(n-1). Technically you are missing the boundary condition here, but with any constant boundary condition you see that the recursion is O(n^2).
Looks about right, but will depend on the base case T(1). Assuming you will do n steps to get T(n) to T(0) and each time the n term is anywhere between 0 and n for an average of n/2 so n * n/2 = (n^2)/2 = O(n^2).

Resources