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

1 e Intro2 Analysis Algo

The document discusses the analysis of algorithms, focusing on time and space complexity as key metrics for efficiency. It explains theoretical analysis, best, average, and worst-case scenarios using the linear search algorithm as an example. The best case time complexity is O(1), while both the worst and average case complexities are O(n).

Uploaded by

hashirkaamran95
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

1 e Intro2 Analysis Algo

The document discusses the analysis of algorithms, focusing on time and space complexity as key metrics for efficiency. It explains theoretical analysis, best, average, and worst-case scenarios using the linear search algorithm as an example. The best case time complexity is O(1), while both the worst and average case complexities are O(n).

Uploaded by

hashirkaamran95
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Introduction to Analysis of Algorithms

To judge the efficiency of an algorithm, we need an analyzing tool.

There are two techniques to measure the efficiency of an algorithm.


 Time Complexity: Time complexity is the time taken by an algorithm to execute.
 Space Complexity: Space complexity is the amount of memory used by an algorithm while
executing.
The analysis of algorithms is to find the complexity of algorithms in terms of time and storage.

In theoretical analysis, the analysis is performed based on the description of the algorithm, or the operation
of the program or function. The theoretical analysis takes all the possible input into consideration and
assumes that the time taken for executing the basic operation is constant.

The primitive or basic operations that take constant time are given as follows.
 Declarations
 Assignments
 Arithmetic Operations
 Comparison statements
 Calling functions
 Return statement
Let us consider the following code.
i =0 #declaration statement
count = 0 #declaration statement
while(i < N): #comparison statement

count +=1 #arithmetic operation

Operations Frequency

Declaration statement 2

Comparison statement N

Arithmetic operations N

Since the arithmetic operation is within the while loop, it will be executed for N number of times. Now the
total operations performed is 2 + N + N = 2 + 2N. When the value of N is large, then the constant values don’t
make any difference and are insignificant. Hence, we ignore the constant values.

Best, Average, and Worst cases


An algorithm can perform differently based on the given input. There are three cases to analyze an algorithm.
They are worst case, average case, and the best case.

Let us consider the linear search algorithm. In linear search algorithm, we search for an element sequentially.

In the following example, we perform linear search on the list list_1 = [4, 6, 7, 1, 5, 2].

Compiled By: [email protected] Page 1


def linear_search(list_1, key):
for i in range(0, len(list_1)):

if key == list_1[i]:

print(key, "is at index", i)

list_1 = [4, 6, 7, 1, 5, 2]
linear_search(list_1, 6)

#Output: 6 is at index 1

Best case: The best case is the minimum time required by an algorithm for the execution of a program. If we
are searching for an element in a list, and it is present at the 0th index, then the number of comparisons to be
performed is 1 time. The time complexity will be O(1) i.e. constant which is the best case time.

Worst case: The worst case is the maximum time required by an algorithm for the execution of a program. If
we are searching for an element in the list of length “n”, and it’s not present in the list or is present at the
ending index, then the number of comparisons to be performed will be “n” times.
Therefore, the time complexity will be O(n), which is the worst case time.

Average case: In average case analysis, we take the average time of all the possible inputs. The average case
time is given by,
Average case time = All possible cases time / Number of cases

In case, if we are searching for an element, and it is present at the first location, then it will take 1 unit of time.
If we are searching for an element, and it is present at the second location, then it will take 2 units of time.
Similarly, if we are searching for an element present at the nth location, then it will take the “n” units of time.

The total number of possible cases will be “n”. Mathematically, we can write
Average time = (1 + 2 + 3... + n)/ n
= (n(n + 1))/(2n)
= (n + 1)/2
Assuming "n" is very large, we ignore the constant terms.
Thus, the average case time can be written as
Average time = n

The best, worst and average case time for linear search algorithm is
Best case B(n) = O(1)
Worst case W(n) = O(n)
Average case A(n) = O(n)

Compiled By: [email protected] Page 2

You might also like