In previous lecture, we were able to define the time complexity for our code using the asymptotic algorithm.
In this section or in this lecture, we are going to determine a case of the implementation of an algorithm.
So there are three cases when implementing the complexity of algorithm is the there are the worse here
actually lets me fix that here.
So there are the worst.
The average.
And best.
So before we go through them, let's look at the search function implementation here.
So now we're going to create the new search function here.
Integer search.
And that's going to take the parameter integer array here and integer.
Integer array.
Size and integer X.
So here we will.
After that we will create a for loop to iterate through the array elements.
So integer E equals zero while the E is less than array size.
And E plus.
Plus.
Or plus.
Plus E.
Okay.
Correct.
And here, uh, the if our x here.
If our x is found, return the index index of x here.
So if our array dot e here is equals to x, then return E here.
This means we found our, uh, searching element here.
So if our x x is not found here.
So if, uh, this loop, this condition is not executed, this means our X is not found here.
So now we're going to create here this, uh, consider this as an else statement here.
So if X is not found, then return, uh, if we then return or not here to be correct.
If x is not found, then return the minus one.
Return minus one here.
As you can see here, this search function, it will find an index of target element.
This is here X from an array, a a r r array containing a R size elements.
So suppose we have the array of here.
Let's actually create the 4255 and.
Let's actually create our array here.
So integer array.
Here.
So let's create our array here with five.
Thanks for.
Here is our array.
And we will make this for example, our array size is going to be nine.
So search, search, our array is going to be R, the array size is going to be nine.
Oops, uh, nine.
And what we want to find is, for example, 97.
Here.
If we run this program.
Here we have no output, but we are code is compiled without an error.
So we have the worst case.
Let's actually start with the learning about the worst case.
Actually, let's first print our here.
So c out here, then print the array dot e here and make this.
Found and re here and after that end line.
And see out.
Element not found.
And new line and line.
Let's run it again.
And as you can see here, we found our array.
So let's 22 and as you can see, we have no 22 in this array.
And as you can see, we got an A finished with exit code because element is not found here.
So let's get started with the worst case analysis.
Average case analysis and best case analysis here.
So worst case analysis is a calculation of the upper bound on the running runtime or of the running
time of the algorithm.
So in the search function.
The upper bound can be an element that does not appear in the IRR.
For instance, here 60.
So it has to iterate through all elements of array IRR variable and still cannot find the element.
So this is our worst.
Case.
We also have the average case here.
Average.
Case.
So average case analysis is a calculation of all possible inputs on the running time of algorithm,
so including an element that is not present in the arrays.
We also have the best case.
The best.
Best case analysis is a calculation of the lower bound on the running time of the algorithm.
So in the search function, the lower bound is the first element of the array, which is 45.
So when we search for element 45, the function will only iterate through R only once.
So the array size doesn't matter here.
You can also do it like that and element not found.
And continuing iterating.
Element not found and iterating.
Here.
Run it again.
And as you can see here, our element not found either here.
That was the worst case scenario.
The worst case scenario is whenever we found out, for example, 25, 21 or 14 here if we have 14.
Of the race as it's going to be nine here.
Our X here is going to be 14, for example.
And as you can see here, after the iterating, one, two, three, four, five, six, seven, eight,
nine, the 14 is found.