# Algorithm Analysis: Best, Worst and Average Case

**Published:**
·
**Last updated:**

**Estimated Reading Time:** 5 minutes

Algorithms are a sequence of decisions we make to solve a problem. Like every decision in life we can make great decisions and really terrible decisions. But it doesn’t end there. Good decisions vary, they can come easy and can take a lot harder process to reach an expected outcome.

There are 3 categories of efficiency an algorithm can fall under when analyzing its usage of resources. They are best case, worst case, and average case.

To give you a mind picture, my friend Jesse and I decided to go on a road trip. We were to travel 300 miles and Jesse assured me we had enough gas for each gas station stops through the journey. Jesse made a good calculation but ignored the possibility of errors which led to an unexpected outcome when we had traveled 160 miles and found that the next gas station wasn’t functional. This was really unfortunate because the next gas station was 80 miles away and we were about to hit the E. We got it to 220 miles through our journey and the car stopped. We wanted to hitchhike through the rest of the journey but after waiting for 2 hours with no passerby we had to decide on walking down to the next gas station with some gallons we had in the car.

2 miles after the next gas station, there’s another station, and another one 1 mile after that. That’s a good reason to believe going to fetch some gas would be a better decision than waiting for hours for a chance to hitchhike. But another condition we were constrained by was that our car runs on diesel and while there’s always petrol in all gas stations, they don’t always have diesel.

So far we’ve chosen footslogging with gallons over hitchhiking. We think it’s a good decision but we have another case here. If the gas station 20 miles away has diesel, that’d be the **best case**, if we have to try the first and second second gas stations to find that only the third has diesel or none of them have diesel, it would be the **worst case**. If the second gas station has diesel and the first didn’t then we don’t have to try the third. That’s an **average case**.

With that understanding, when giving an array of length **n**, when **n = 5** and our array items are **23, 34, 13, 44, 18**, we are to write an algorithm to find a number in the list of array items. By performing a linear search, we will have our best case if 23 is the number to be searched as it is the first item on the array.

${C}_{\mathrm{best}}\left(n\right)=1$

If we then have to search for 18 or any other number like 58 which doesn’t exist in the list, it’d be a worst case efficiency.

$k=18$

${C}_{\mathrm{worst}}\left(n\right)=n$For every possibility of worst case either the item is found in the last position or not found at all we go through the **n** number of elements.

34, 13, and 44 are the average cases of the search. To find any of those items at a position **i** of the array, we derive the probability of success and failure. Given that:

$\text{where,}\phantom{\rule{20px}{0ex}}0\le p\le 1$

$\text{Probability of Unsucessful Search}=1-p$

The probability to find an item that is neither the first nor last item of the array at a position **i** in the array then becomes:

and the probability of reoccurence of the item in position **i** would be:

with **1 - p** added as probability of unsuccesful search.

Simplifying the above equation we get:

$\frac{p}{n}[1+2+\dots +i+\dots n]+n(1-p)$and substituting the series with $\frac{n(n+1)}{2}$, we have:

$\frac{p}{n}\left[\frac{n(n+1)}{2}\right]+n(1-p)\phantom{\rule{0ex}{0ex}}\frac{p(n+1)}{2}+n(1-p)$we have a simplified addition of successful search and unsuccessful search for an average case. The probability *(p)* of a successful search (certainty) is **1** and unsuccessful search (impossibility) is **0**. To start with the successful search we substitute p for 1.

For unsuccessful search, p is substituted for 0:

$\text{Unsuccessful Search}\to \frac{0(n+1)}{2}+n(1-0)\phantom{\rule{0ex}{0ex}}{C}_{\mathrm{average}}\left(n\right)=n$and the unsuccessful search of average case has led right back to being the same as worst case.

${C}_{\mathrm{unsuccessful\; average}}\left(n\right)={C}_{\mathrm{worst}}\left(n\right)$The best case from the examples is the optimum solution to the problems while every other solutions are feasible. Feasible solutions are the set of input values that satisfy all the constraints of an algorithm. Hence, going to the first and second gas stations before finding diesel at the third still makes the first and second feasible solutions while the third becomes the optimum solution.