I have an sorted array of strings: eg: ["bar", "foo", "top", "zebra"] and I want to search if an input word is present in an array or not.
eg:
search (String[] str, String word) {
// binary search implemented + string comaparison.
}
Now binary search will account for complexity which is O(logn), where n is the length of an array. So for so good.
But, at some point we need to do a string compare, which can be done in linear time.
Now the input array can contain of words of different sizes. So when I am calculating final complexity will the final answer be O(m*logn) where
m
is the size of word we want to search in the array, which in our case is "zebra" the word we want to search?
Yes, your thinking as well your proposed solution, both are correct. You need to consider the length of the longest String too in the overall complexity of String searching.
A trivial String compare is an
O(m)
operation, wherem
is the length of the larger of the two strings.But, we can improve a lot, given that the array is sorted. As user "doynax" suggests,
So, overall
m
number of character comparisons would have to be done till the end of the string, if found OR else not even that much(if fails at early stage).So, the overall complexity would be
O(m + log n)
.