How can we modify almost any algorithm to have a good best-case running time? -
this question introduction algorithms cormen. isn't homework problem instead self-study.
i have thought lot , searched on google. answer can think of are:-
- use algorithm.
- give best-case inputs
- use better computer run algorithm
but don't think these correct. changing algorithm isn't same making algorithm have better performance. using better computer may increase speed algorithm isn't better. question in beginning of book think simple overlooking.
so how can modify algorithm have best-case running time?
you can modify algorithm have best case time complexity of o(n)
adding special case, if input matches special case - return cached hard coded answer (or other obtained answer).
for example, sort, can make best case o(n)
checking if array sorted - , if is, return is.
note not impact average or worst cases (assuming not better o(n)
), , improve algorithm's best case time complexity.
note: if size of input bounded, same optimization makes best case o(1)
, because reading input in case o(1)
.
Comments
Post a Comment