Reputation: 57
I am studying time complexity for binary search, ternary search and k-ary search in N elements and have come up with its respective asymptotic worse case run- time. However, I started to wonder what would happen if I divide N elements into n ranges (or aka n-ary search in n elements). Would that be a sorted linear search in an array which would result in a run-time of O(N)? This is a little confusing. Please help me out!
Upvotes: 3
Views: 5224
Reputation: 6781
What you say is right.
For a k-ary
search we have:
k-1
checks in boundaries to isolate one of the k
ranges.Hence the time complexity is essentially O((k-1)*log_k(N))
where log_k(N)
means 'log(N)
to base k
'. This has a minimum when k=2
.
If k = N
, the time complexity will be: O((N-1) * log_N(N))
= O(N-1)
= O(N)
, which is the same algorithmically and complexity-wise as linear search.
Translated to the algorithm above, it is:
N-1
checks in boundaries (each of the first N-1
elements) to isolate one of the N
ranges. This is the same as a linear search in the first N-1
elements.Upvotes: 4