1 e Intro2 Analysis Algo
1 e Intro2 Analysis Algo
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
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.
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].
if key == list_1[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)