big o - what the running time of this recursive algorithm? -
search_item(a,item,c) index <- c if (a[index] > item) index <- 2 * index search_item(a, item, index) else if (a[index] < item) index <- 2 * index + 1 search_item(a, item, index) else return index
you gave little (no) information. sad, give answer , hope show more effort in other questions, show effort in answering question.
the algorithm posted seems incomplete. think there return
s missing. however:
the simplest complexity analysis this:
work on array a
. let n
number of elements in a
. in every recursive call either multiplying current index 2 (and make recursive call) or return current index.
assuming algorithm correct, can have k
recursive calls 2k < n
. k < log₂(n)
holds.
this means algorithm has time-complexity of o(log n)
.
since algorithm named "search" algorithm, looks array a
representation of binary search tree , search algorithm recursive binary search.
fits complexity calculated.
Comments
Post a Comment