We can have three cases to analyze an algorithm:
1) Worst Case
2) Average Case
3) Best Case
Let us consider the following implementation of Linear Search.
Best case: Clearly the best case occurs when x is the first element in the array A. That is Θ(1)
Worst case: Clearly the worst case occurs when x is the last element in the array That is Θ(n)
Average case: Here we assume that searched element appear array A, and it is equally likely to occur at any position in the array. Here the number of comparisons can be any of numbers 1,2,3,…,n, and each number occurs with the probability p=1/n
The average case analysis is not easy to do in most of the practical cases and it is rarely done. In the average case analysis, we must know (or predict) the mathematical distribution of all possible inputs.
#Complexity #Algorithm #BestCase #WorstCase #AverageCase #Probyto #ProbytoAI
Subscribe and follow us for latest news in Data Science and Machine learning and stay updated!