Best Case, Worst Case and Average Case Analysis of an Algorithm
To define an algorithm, To define the events in the life of an algorithm , We have , Best Case
Worst Case and Expected Case. And along with that, I 've packed one more thing into this
video : The definition of Log. If you watch this video till the end , Then you will find out what this
'Log ' really is. 1. . . 5. . . 7. . . 9 and 24 are the numbers in it; They 're in ascending order , You
can see for yourself. If you know even a bit of maths, You 'll know that it is in an ascending order.
Now what I say is that I 'll give a number : 'A' And I 'd like you to tell me If this number exists
within the array , or not. Suppose the value of A is 8. So what will be your answer ? Yes.
Meaning 1. If A is. . . Sorry, your answer will be no , because it is n't there. If the value is 9, What
will the answer be ? Your answer will Algo 1 is a simple person. It does n't have much of a brain.
It is comparing it with all the numbers. Is this the best way to do this work ? Obviously not.
Because Algo. 1 is lucky , He will get A=1. It will tell us in the first comparison itself. In one
comparison only.
If Algo 1 is in luck, The time needed is ' k ' - T=k. This means that it does n't depend on 'n '. Take
a 10-element array, take a single element array or take a 10,000 element array. It only has to
make one comparison because it is only searching for the first element in the array. Now, AlGo 1
's luck is bad. Till now, he was fortunate ; But now he 's not so lucky anymore. Average Case
complexity is equal to. . . the sum of the run time for the total number of possibilities. The O
( Sum of all possible run times divided by the number of possibility ) is O ( n ) The average case
complexity is the sum. Average Case is equal. to. . . The sum of all. possible run. times divided.
by the total. number of possible run time. So for an array size of 5, We saw six cases. 't ' ; I 'll
calculate ' O ' later. n+1 If 'n ' is the size of the array , Then there is 'n+1 '' number of
possibilities. 'n' possibilities is when there is 1st element, 2nd element, 3rd element, 4th
element, 5th element and 6th element. If the element is here, How many comparisons will it
have to make ? It will have to do. . . 1. . . 2. . . 3 comparisons. I 've taken ' k ' as common out of
everything. I removed this because this is different. And this I have added separately.
AP is Arithmetic Progression and GP is a geometric progression. AP is used in 'O ' a lot. APs are
made as well as GPs sometimes. When there are questions on 'O' APs and GPs are used in the
answers to questions on O. The formula of Average Case Complexity is All possible run times
divided by the total number of possibilities. The Average Case Time is not generally asked for a
unique algorithm. It can be asked for this type of algorithm , A simple algorithm that compares
all of these. So what is the Average Case. Complexity ? O ( n ). Algo 1 was making 'n '
comparisons. Now we 'll see how many comparisons this will make. Algo 2 is a cunning person.
It will match the first and last element and match it first. It made one comparison For a size of
10 array , As well as for an array of size 100. It is making the same comparison for all sizes of
array.
The midpoint between 1 and 100 will be 50. Then you will discard this entire array that has
elements greater than 50. If there are an even number of elements here, there are 2-2-4,2-6
To define an algorithm, To define the events in the life of an algorithm , We have , Best Case
Worst Case and Expected Case. And along with that, I 've packed one more thing into this
video : The definition of Log. If you watch this video till the end , Then you will find out what this
'Log ' really is. 1. . . 5. . . 7. . . 9 and 24 are the numbers in it; They 're in ascending order , You
can see for yourself. If you know even a bit of maths, You 'll know that it is in an ascending order.
Now what I say is that I 'll give a number : 'A' And I 'd like you to tell me If this number exists
within the array , or not. Suppose the value of A is 8. So what will be your answer ? Yes.
Meaning 1. If A is. . . Sorry, your answer will be no , because it is n't there. If the value is 9, What
will the answer be ? Your answer will Algo 1 is a simple person. It does n't have much of a brain.
It is comparing it with all the numbers. Is this the best way to do this work ? Obviously not.
Because Algo. 1 is lucky , He will get A=1. It will tell us in the first comparison itself. In one
comparison only.
If Algo 1 is in luck, The time needed is ' k ' - T=k. This means that it does n't depend on 'n '. Take
a 10-element array, take a single element array or take a 10,000 element array. It only has to
make one comparison because it is only searching for the first element in the array. Now, AlGo 1
's luck is bad. Till now, he was fortunate ; But now he 's not so lucky anymore. Average Case
complexity is equal to. . . the sum of the run time for the total number of possibilities. The O
( Sum of all possible run times divided by the number of possibility ) is O ( n ) The average case
complexity is the sum. Average Case is equal. to. . . The sum of all. possible run. times divided.
by the total. number of possible run time. So for an array size of 5, We saw six cases. 't ' ; I 'll
calculate ' O ' later. n+1 If 'n ' is the size of the array , Then there is 'n+1 '' number of
possibilities. 'n' possibilities is when there is 1st element, 2nd element, 3rd element, 4th
element, 5th element and 6th element. If the element is here, How many comparisons will it
have to make ? It will have to do. . . 1. . . 2. . . 3 comparisons. I 've taken ' k ' as common out of
everything. I removed this because this is different. And this I have added separately.
AP is Arithmetic Progression and GP is a geometric progression. AP is used in 'O ' a lot. APs are
made as well as GPs sometimes. When there are questions on 'O' APs and GPs are used in the
answers to questions on O. The formula of Average Case Complexity is All possible run times
divided by the total number of possibilities. The Average Case Time is not generally asked for a
unique algorithm. It can be asked for this type of algorithm , A simple algorithm that compares
all of these. So what is the Average Case. Complexity ? O ( n ). Algo 1 was making 'n '
comparisons. Now we 'll see how many comparisons this will make. Algo 2 is a cunning person.
It will match the first and last element and match it first. It made one comparison For a size of
10 array , As well as for an array of size 100. It is making the same comparison for all sizes of
array.
The midpoint between 1 and 100 will be 50. Then you will discard this entire array that has
elements greater than 50. If there are an even number of elements here, there are 2-2-4,2-6