0% found this document useful (0 votes)
36 views

L5 - Time & Space Complexity-1.1

Uploaded by

Siddharth Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

L5 - Time & Space Complexity-1.1

Uploaded by

Siddharth Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

How To

Calculate Time O(n^2)

Operations
Complexity O (n log n)

O(n)

Elements
What is Time Complexity?
You can getTopic/Course
the time complexity by “counting” the number of operations performed
by your code.

Time complexity of an algorithm quantifies the amount of time taken by an algorithm


to run as a function of the length of the input.

This time complexity is defined as a function of the input size n using Big-O notation.
n indicates the input size, while O is the worst-case scenario growth rate function.

We use the Big-O notation to classify algorithms based on their running time or
space (memory used) as the input grows.

The O function is the growth rate in function of the input size n.


Time and space complexity depends on lots of things like hardware,
operating system, processors, etc.
Topic/Course

However, we don't consider any of these factors while analyzing the


algorithm. We will only consider the execution time of an algorithm.

Lets start with a simple example. Suppose you are given an array and an
integer and you have to find if exists in array .

Simple solution to this problem is traverse the whole array and check if the
any element is equal to x
for(i=0;i<lengthA[n];i++)
{
If(A[i]==x)
return true;
else
return false;
}
Consider the above problem.

Each of the operation in computer take approximately constant time.

Let each operation takes ‘c’ time. The number of lines of code executed is actually depends on
the value of ‘x’.

The worst case scenario: The if condition will run ‘x’ if the array isn’t found

Best Case scenario: The if case will be run only once (element will be found in the first pass

The space requirement is constant. It is so because we store only an array and no additional
space is required
No matter how big the constant is and how slow the linear
increase is, linear will at some point surpass constant.

There are many more runtimes than this. Some of the


Topic/Course
most common ones are O(log N), O(N log N), O(N),
O(N2) and 0(2N).

There's no fixed list of possible runtimes, though.

You can also have multiple variables in your runtime.


For example, the time to paint a fence that's w meters
wide and h meters high could be described as O (wh).

If you needed players of paint, then you could say that the
time is O (whp).
Big 0, Big Theta, and Big Omega
big 0, big theta, and big omega are used to describe runtimes.

O (big Oh): Big O describes an upper bound on the time. An algorithm that prints all the values
Topic/Course
in an array could be described as O(N), but it could also be described as O(N2), O(N3), or 0( 2N)
(or many other big O times).

The algorithm is at least as fast as each of these; therefore they are upper bounds on the
runtime. This is similar to a less-than-or-equal-to relationship.

A simple algorithm to print the values in an array is O(N)

big omega: Omega is the equivalent concept but for lower bound. Printing the values in
an array is O(N) as well as O(log N) and 0(1). After all, you know that it won't be faster than
those runtimes.

big theta: Theta means both O and Omega. That is, an algorithm is Theta(N) if it is both O(N)
and Omega( N)
Note that the time to run is a function of the length of the input and not the
actual execution time of the machine on which the algorithm is running on.
Topic/Course

This is what the concept of asymptotic runtime, or big O time, means. We


could describe the data transfer "algorithm" runtime as:

Electronic Transfer: O(s), where s is the size of the file. This means that the
time to transfer the file increases linearly with the size of the file. (Yes, this is
a bit of a simplification, but that's okay for these purposes.)

Airplane Transfer: 0(1) with respect to the size of the file. As the size of the
file increases, it won't take any longer to get the file to your friend. The time
is constan
How To

Calculate Time O(n^2)

Operations
Complexity O (n log n)

O(n)

Elements
Big O Cheatsheet
Big O Notation Name Examples

Topic/Course
O(1) Constant Odd or Even number
O(log n) Finding element on sorted array with binary
Logarithmic search
O(n) Linear Find max element in unsorted array

O(n log n) Linearithmic Sorting elements in array with merge sort


O(n2) Quadratic Sorting array with bubble sort
O(n3) Cubic 3 variables equation solver
O(2n) Exponential Find all subsets
O(n!) Factorial Find all permutations of a given set/string
Predict the time complexity for the given below code snippet
int fun()
{
for(i = 1; i < n; i++)
{
cout << “SIX PHRASE”;
}
return 0;
}

A) O(n) B) O(log n) C) O (sqrt n) D) O(n * n)


Predict the time complexity for the given below code snippet

int fun()
{
for(i = 1; i <= sqrt(n); i++)
{
cout << “SIX PHRASE”;
}
return 0;
}

A) O(n) B) O(log n) C) O (sqrt n) D) O(n * n)


Predict the time complexity for the given below code snippet

int fun()
{
for(i = 1; i*i <= n; i++)
{
cout << “SIX PHRASE”;
}
return 0;
}

A) O(n) B) O(log n) C) O (sqrt n) D) O(n * n)


Predict the time complexity for the given below code snippet
int fun()
{
for(i = 1; i <= n; i=i*2)
{
cout << “SIX PHRASE”;
}
return 0;
}

A) O(n) B) O(log n) C) O (sqrt n) D) O(n * n)


Predict the time complexity for the given below code snippet

int fun()
{
int n;
while(n > 1)
{
n = n / 2;
}
return 0;
}

A) O(n) B) O(log n) C) O (sqrt n) D) O(n * n)


Predict the time complexity for the given below code snippet
int fun()
{
for(i = 1; i < n; i++)
{
for(j = 1; j < n; j++)
{
cout << “SIX PHRASE”;
}
}
return 0;
}

A) O(n) B) O(log n) C) O (sqrt n) D) O(n * n)


Predict the time complexity for the given below code snippet
int fun()
{
for(i = 1; i < n; i++)
{
for(j = 1; j < n; j++)
{
cout << “SIX PHRASE”;
break;
}
}
return 0;
}

A) O(n) B) O(log n) C) O (sqrt n) D) O(n * n)


Predict the time complexity for the given below code snippet
int fun()
{
for(i = 1; i < n; i++)
{
for(j = 1; j < n; j=j+i)
{
cout << “SIX PHRASE”;
}
}
return 0;
}

A) O(n) B) O(log n) C) O (n * log n) D) O(n * n)


Predict the time complexity for the given below code snippet
int fun()
{
int i, j;
for(i = 1; i < n; i++)
{
for(j = 1; j < n; j=j*2)
{
cout << “SIX PHRASE”;
}
}
return 0;
}
A) O(n) B) O(log n) C) O (n * log n) D) O(n * n)
Predict the time complexity for the given below code snippet
int fun()
{
int i, j;
for(i = 1; i < n; i++)
{
for(j = n; j > 1; j--)
{
cout << “SIX PHRASE”;
}
}
}

A) O(n) B) O(log n) C) O (n * log n) D) O(n * n)


Predict the time complexity for the given below code snippet
int fun()
{
int i, j, a = 0, b = 0;

for(i = 1; i < n; i++)


a = a + i;
for(j = 1; j < m; j++)
b = b + j;

return 0;
}

A) O(n) B) O(m) C) O (n + m) D) O(n * m)


Predict the time complexity for the given below code snippet
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
{
for(k = 1; k <= 20; k++)
{
cout << “SIX PHRASE”;
}
}
}

A) O(n) B) O(log n) C) O (sqrt n) D) O(n * n)


Predict the time complexity for the given below code snippet
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i*i; j++)
{
for(k = 1; k <= (n/2); k++)
{
cout << “SIX PHRASE”;
}
}
}

A) O(n) B) O(n ^ 2) C) O (n ^ 3) D) O(n ^ 4)


Predict the time complexity for the given below code snippet

for(i = n/2; i >=1; i--)


{
for(j = 1; j <= n; j=j*2)
{
for(k = 1; k <= n; k=k*2)
{
cout << “SIX PHRASE”;
}
}
}

A) O(n) B) O(n * n) C) O (sqrt n) D) O(n*(log n)^2)


Predict the time complexity for the given below code snippet

for(i = n/2; i <= n; i++)


{
for(j = 1; j +n/2<= n; j++)
{
for(k = 1; k <= n; k=k*2)
{
cout << “SIX PHRASE”;
}
}
}

A) O(n) B) O(log n) C) O (n * n) D) O(n^2 (log n))


Predict the time complexity for the given below code snippet
int fun()
{
int n, ans = 0;
cin >> n;
while(n > 0)
{
ans += n % 10;
n /= 10;
}
return 0;
}

A) O(log2 n) B) O(log3 n) C) O(log10 n) D) O(n)


Predict the time complexity for the given below code snippet

for(i = n/2; i >=1; i--)


{
for(j = 1; j <= n/2; j++)
{
for(k = 1; k <= n; k=k*2)
{
cout << “SIX PHRASE”;
}
}
}

A) O(n) B) O(log n) C) O (n * n) D) O(n^2 (log n))


Predict the time complexity for the given below code snippet

int fun(int n)
{
if(n > 1)
return fun(n – 1);
}

A) O(n) B) O(log n) C) O (n * log n) D) O(n * n)


Predict the time complexity for the given below code snippet

int fun()
{
for(i = 1; i < n; i++)
{
for(j = 1; j < sqrt(n); j++)
{
cout << “SIX PHRASE”;
}
}
return 0;
}

A) O(n) B) O(n * sqrt n) C) O (n * log n) D) O(n * n)


THANK YOU

You might also like