Idea of Efficiency
Idea of Efficiency
Algorithm efficiency is related to the time taken by a program for execution and space used by the
algorithm.
Performance of algorithm depends on many internal and external factors.
Time Complexity:
- It is the number of elementary instructions that the program executes.
- Number depends on the size of the input data.
- The time complexity of algorithms is most commonly expressed using the big O notation.
Big O Notation:
- The growth rate determines the algorithm’s performance when its input size grows.
- It is the upper bound of an algorithm’s performance i.e. if we say an algorithm takes O(n2) time: this
means that this algorithm will carry out its task taking at most n2 steps for input size n.
- Performance of the algorithms is inversely proportional to the wall clock time. Programs with a bigger
O run slower than programs with a smaller O.
Dominant term: if the algorithm’s performance is an2 +bn +c then we can say an2 is the dominant
term which has maximum impact on algorithm’s performance
Calculating complexity (Assumption: All steps take precisely take same time to execute.)
Running time of loop is equal to the running time of statement inside the loop (including tests)
multiplied by the number of iteration.
1. Loops:
for i in range(n):
m=m+2
So, the total time taken by above loop to execute itself is:
Total time =c*n=cn
c=time taken by n steps n=no of iterations
i.e complexity is O(n)
2. Nested Loops:
for i in range(n):
for j in range(n):
L=L+1
So, the total time taken by above nested loop to execute is:
Total time=c*n*n=cn2
I.e. complexity is O(n2)
3. Consecutive Statements:
a=a-1
for i in range(n):
m=m+2
for j in range(n):
for k in range(n):
b=b+1
4. if-then-else statements:
if(len(list1)!=len(list2):
return False
else:
for i in range(n):
if(list1[i]!=list2[i]):
return False
5. Logarithmic Complexity: It means that an algorithm’s performance has logarithmic factor e.g. Binary
search O(log n)
Wall Clock Time: The wall-clock time is not the number of seconds that the process has spent on the
CPU; it is the elapsed time, including time spent waiting for its turn on the CPU (while other processes
get to run).
Performance of algorithm is inversely proportional to the wall clock time it records for a given input size.
import time
start=time.time( )
num = int(input("Enter a number: "))
factorial = 1
if num < 0:
print("factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1, num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
end=time.time()
t=end-start
print("Total time taken: ", t)
OUTPUT:-
Enter a number: 5
The factorial of 5 is 120
Total time taken: 5.615321159362793
Recursive Program
import time
def factorial(n):
if n == 1:
return n
else:
return n*factorial(n-1)
start=time.time()
num=int(input("enter the number: "))
if num < 0:
print("factorial does not exist for negative numbers")
elif num==0:
print("The factorial of 0 is 1")
else:
print("The factorial of ",num," is ", factorial(num))
end=time.time()
t=end-start
print("Total time taken: ", t)
OUTPUT:-
Recursive program is taking less time than without recursion program. So, Program with recursion is
efficient. Therefore, we can conclude that Performance of algorithm is inversely proportional to the wall
clock time.
Answer = Algorithm efficiency is related to the time taken by a program for execution and space used by
the algorithm.
Answer = It is the upper bound of an algorithm’s performance i.e. if we say an algorithm takes O(n2)
time: this means that this algorithm will carry out its task taking at most n2 steps for input size n.
Answer =
Internal Factors specify algorithm’s efficiency in terms of:
- Time required to run
- Space required to run
Answer =
External Factors affecting algorithm efficiency are:
- Size of the input to the algorithm
- Speed of the computer on which it is run
- Quality of the compiler
Answer = O (log n)
(a)
n=int(input("Enter the number to check"))
flag=0
for i in range(2,n):
if n%i==0:
flag=1
break
if flag:
print(n,"is Not a prime number")
else:
print(n," is prime number")
Answer = O(n)
(b)
n=int(input("Enter the number to check"))
flag=1
i=2
while(i*i<=n):
if n%i==0:
flag=0
break i=i+1
if flag:
print(n,"is Not a prime number")
else:
print(n," is prime number")
Answer = O(n)
(c)
for i in range(n):
a=i+i+2
print(a)
for j in range(m):
b=b*4
print(b)
Answer = O(n+m)
(d)
for i in range(n):
for j in range(n):
a=i+j
print(a)
for k in range(n):
print(k)
Answer = O(n2)
Answer =
(i) Redundant computations or unnecessary use of storage should be avoided.
(ii) Inefficiency due to late termination should be taken care of.
Q15. Given the following array, which search will find the value 18 in least steps?
3 10 18 22 35
Q16. Given the following array, which search will find the value 3 in least steps?
3 10 18 22 35
i=1
while i<=n:
j=1
while j<=n:
k=1
while k<=n:
print(i, j, k)
k=k+1
j=j+1
i=i+1
Answer = O (n3)
Q18.
(a) What is the worst case complexity of the following code fragment?
for x in range(a):
statements
for y in range(b):
for z in range(c):
statements
Thankyou!!!!!