- Best-case time complexity is the minimum amount of time an algorithm takes for a given input. It represents the most favorable scenario.
- Average-case time complexity is the expected time taken over all possible inputs. It accounts for different cases but is often more challenging to analyze.
- Worst-case time complexity is the maximum amount of time an algorithm takes for a given input. It represents the most unfavorable scenario.
Example code for worst-case time complexity:
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
The worst-case time complexity for this linear search is O(n), which occurs when the target element is not in the array or is the last element.