Reputation: 63
Does every algorithm has a 'best case' and 'worst case' , this was a question raised by someone who answered it with no ! I thought that every algorithm has a case depending on its input so that one algorithm finds that a particular set of input are the best case but other algorithms consider it the worst case.
so which answer is correct and if there are algorithms that doesn't have a best case can you give an example ?
Thank You :)
Upvotes: 1
Views: 1070
Reputation: 5277
Best Case input is the casein which your code would take the least number of procedure calls
. eg. You have an if
in your code and in that, you iterate for every element and no such functionality in else
part. So, any input in which the code does not enter if
block will be the best case input
and conversely, any input in which code enters this if will be worst case
for this algorithm.
If, for any algorithm, branching or recursion or looping causes a difference in complexity factor
for that algorithm, it will have a possible best case
or possible worst case
scenario. Otherwise, you can say that it does not or that it has similar complexity
for best case or worst case.
Talking about sorting algorithms, lets take example of merge
and quick
sorts. (I believe you know them well, and their complexities for that matter).
In merge sort
every time, array is divided into two equal parts thus taking log n
factor in splitting while in recombining, it takes O(n)
time (for every split, of course). So, total complexity is always O(n log n)
and it does not depend on the input. So, you can either say merge sort has no best/worst case conditions or its complexity is same for best/worst cases.
On the other hand, if quick sort (not randomized, pivot always the 1st element) is given a random input, it will always divide the array in two parts, (may or may not be equal, doesn't matter) and if it does this, log factor of its complexity
comes into picture (though base won't always be 2). But, if the input is sorted already (ascending or descending) it will always split it into 1 element + rest of array
, so will take n-1
iterations to split the array, which changes its O(log n)
factor to O(n)
thereby changing complexity to O(n^2)
. So, quick sort will have best and worst cases with different time complexities.
Upvotes: 3
Reputation: 15004
I think its reasonable to say that most algorithms have a best and a worst case. If you think about algorithms in terms of Asymptotic Analysis you can say that a O(n) search algorithm will perform worse than a O(log n) algorithm. However if you provide the O(n) algorithm with data where the search item is early on in the data set and the O(log n) algorithm with data where the search item is in the last node to be found the O(n) will perform faster than the O(log n).
However an algorithm that has to examine each of the inputs every time such as an Average algorithm won't have a best/worst as the processing time would be the same no matter the data.
If you are unfamiliar with Asymptotic Analysis (AKA big O) I suggest you learn about it to get a better understanding of what you are asking.
Upvotes: 1
Reputation: 1402
No, not every algorithm has a best and worst case. An example of that is the linear search to find the max/min element in an unsorted array: it always checks all items in the array no matter what. It's time complexity is therefore Theta(N) and it's independent of the particular input.
Upvotes: 4
Reputation: 881333
Well, I believe every algorithm has a best and worst case though there's no guarantee that they will differ. For example, the algorithm to return the first element in an array has an O(1)
best, worst and average case.
Contrived, I know, but what I'm saying is that it depends entirely on the algorithm what their best and worst cases are, but the cases will exist, even if they're the same, or unbounded at the top end.
Upvotes: 1