You are on page 1of 8

Chapter 5: Idea of Algorithm Efficiency

What is Algorithm:
An algorithm is well-defined and systematic or step
by step method to complete specific task.

What is Efficiency of program:


It means a manner of programming that, when the
program is executed, it uses a low amount of overall
resources like:
1)Memory used by algorithm
2)Time needed to run complete algorithm.
It’s important to measure efficiency of algorithm before applying it on large scale.
Factors to analysis efficiency of algorithm:
Performance of algorithm depends on many Internal and External factors :
Internal Factors: Time and memory required to run program.
External Factors: Size of input to the algorithm, speed to computer.
External factors are controllable, so many internal factors are studied as measured for
algorithms efficiency.
We will determine efficiency of algorithm in terms of computational complexity.

Computational complexity: computation + complexity


Computation : Its involves the problems to be solve and algorithm to solve them.
Complexity : Its involves study of factors to determine how much resource(time to run +
memory) is necessary for algorithm to run efficiently. In this “time complexity” is more
important.
How Many way to evaluate efficiency of program:
1.Program efficiency in term of Time:
It is  a measure of amount of time for an algorithm to execute, and also called Time
Complexity.

2.Program efficiency in term of number of operations:


Big O notation- Its allow us to measure the time and space Complexity of our code. It
determines the algorithm’s performance when its input size grows. Big-O is a function with a
parameter N where N is the size of input to the algorithm. Example: O(n) or O(n2) .
Dominant term : While describing the growth rate of algorithm, we simply consider the term
which is going to affect the most on the algorithms performance. This term known as dominant
term.
How to compute runtime algorithm complexity:
1.Loop:
for i in range(n):
m=m+2
(All the steps in loop take constant time C) & (loop is executed N times)
Total time = c*n =cn
O(n)

2.Nested Loop:
for i in range(n):
for j in range(n):
m=m+2
(Steps in red will take cn times)(outer loop executed n times)
Total time= cn*n = cn2
O(n2 )
3.Consecutive statement:
x=x+1 # time=a
for i in range(n): # time= cn
m=m+2
for i in range(n): # time= bn2
for j in range(m):
m=m+2
Total time = a+cn+bn2 = O(n2)[Considering only the dominant term]

4. If then else statement:


if len(x) !=len(y) #a
return false #b
else:
for i in range (n): #(c+d)*n
if x[i]!=y[i]: #c
return false #d

Total time=a+b+(c+d)*n = O(n)


Best, Average & Worst case complexity in Linear search:
Example list=[40,10, 60,100,80,50, 30 ,70,90,20]
Case Means Comparisons in terms of N
Base case The element being searched may be found at the O(1)
first position.
Worst case The element being searched may be present at O(n)
the last position or not present in the list at all.
Average case The element being searched may be found at the O(n/2)
center position.

Linear search Vs Binary search:


Linear Search
 It searches throughout the list from the beginning
to the end.
 Every element in the array/list is compared to the
element that needs to be searched.
 The elements don't need to be arranged in a
specific/sorted order.
 It is based on sequential approach.
 It is preferable to use it with small-sized data sets.
 It is less efficient when the size of the array/list is large.
 The worst- case complexity to find an element is O(n) where ‘n’ is the number of elements.
 The best-case complexity to find an element is O(1).
Binary Search
 The list to perform binary search on, should be sorted.
 The position of the element to be searched is found by first finding the middle element.
 It is based on divide and conquer approach.
 It is used with large sized datasets.
 It is more efficient on large datasets.
 The worst-case complexity is O(log2n), where ‘n’ is size of the List.
 The best-case complexity to find an element is O(1).

Chapter 5: Idea of Algorithm Efficiency (Worksheet1)


Q1: Write a function to create multiplication table and count the Time Complexity or numbers
of seconds taken by the program.
Q2: Write a program to input a number and count the occurrence of that number in the given
list. And count the Time Complexity.
B = [34,21,3,12,34,56,76,5,4,21,12,34]

You might also like