Best case, average case, and worst-case time complexities refer to the different running times that an algorithm can have, depending on the input.
Best case time complexity is the best-possible running time for an algorithm, given a certain input. For example, if you have a search algorithm that performs a binary search through a sorted array, the best-case time complexity would be O(1), which means that the algorithm would find the desired element in the array in one operation.
Average case time complexity is the average running time for an algorithm, calculated by considering all the possible inputs. For example, if you have a search algorithm that performs a linear search through an unsorted array, the average-case time complexity would be O(n/2), which is simplified to O(n) as the leading term n is dominating.
Worst-case time complexity is the worst-possible running time for an algorithm, given a certain input. For example, if you have a search algorithm that performs a linear search through an unsorted array, the worst-case time complexity would be O(n) meaning that the algorithm would have to look through every element in the array to find the desired element.
Best, average and worst-case time complexities are referred to using Big O notation, which is a mathematical notation used to describe the upper bound of an algorithm's running time, as a function of input size.
Take for example a problem of finding an element in an unsorted array, possible solutions are Linear Search and Binary search.
- Linear Search: in this case, the algorithm will look through each element of the array one by one till it finds the desired element. The best-case time complexity is O(1), if the desired element is the first element, the average-case time complexity is O(n/2) (simplified to O(n)) as the algorithm would have to search through half of the array to find the desired element on average. The worst-case time complexity is O(n), as the algorithm would have to search through every element in the array to find the desired element.
- Binary Search: This is a searching algorithm that works on the principle of divide and conquer. Suppose we want to search for a target value in a sorted array, in this case, the algorithm will check the middle element first and then decide with which half of the array it should proceed. As a result, the number of operations will be log2(n), which is the number of bits required to represent the array size n using a binary number, best-case and average-case time complexity will be O(log n), whereas the worst case will be O(log n) as well, at the very worst the algorithm would have to search through every bit of the array, which still increases very slowly as the array size increases.
As we can see from the examples, the best, average and worst-case time complexities can vary for different algorithms even when solving the same problem. It's essential to understand these concepts to have a good understanding of how well an algorithm will perform under different conditions.