Can someone give me example of an algorithm that has square root(n) time complexity. What does square root time complexity even mean?
Time Complexity: O(√ n).
They are not equivalent: sqrt(N) will increase a lot more quickly than log2(N). There is no constant C so that you would have sqrt(N) < C. log(N) for all values of N greater than some minimum value. So you need to take the logarithm(!) of sqrt(N) to bring it down to the same order of complexity as log2(N).
Algorithm. Take a reasonable guess (approximate root) for the square root. Add the approximate root with the original number divided by the approximate root and divide by 2. Continue step 2 until the difference in the approximate root along the iterations is less than the desired value (or precision value).
we can clearly say that sqrt(n) is larger then logn.
O(N^(1/2))
evaluations where the size of input is N.O(sqrt(n))
time, Grover's algorithm is one which takes that much time. Grover's algorithm is a quantum algorithm for searching an unsorted database of n entries in O(sqrt(n))
time. Let us take an example to understand how can we arrive at O(sqrt(N))
runtime complexity, given a problem. This is going to be elaborate, but is interesting to understand. (The following example, in the context for answering this question, is taken from Coding Contest Byte: The Square Root Trick , very interesting problem and interesting trick to arrive at O(sqrt(n))
complexity)
Given A, containing an n elements array, implement a data structure for point updates and range sum queries.
The naive solution uses an array. It takes O(1)
time for an update (array-index access) and O(hi - lo) = O(n)
for the range sum (iterating from start index to end index and adding up).
query(2, 14) = A[2] + A[3]+ (A[4] + A[5] + A[6] + A[7]) + (A[8] + A[9] + A[10] + A[11]) + A[12] + A[13] + A[14] ; query(2, 14) = A[2] + A[3] + S[1] + S[2] + A[12] + A[13] + A[14] ; query(2, 14) = 0 + 7 + 11 + 9 + 5 + 2 + 0; query(2, 14) = 34;
def update(S, A, i, k, x): S[i/k] = S[i/k] - A[i] + x A[i] = x
def query(S, A, lo, hi, k): s = 0 i = lo //Section 1 (Getting sum from Array A itself, starting part) while (i + 1) % k != 0 and i <= hi: s += A[i] i += 1 //Section 2 (Getting sum from Slices directly, intermediary part) while i + k <= hi: s += S[i/k] i += k //Section 3 (Getting sum from Array A itself, ending part) while i <= hi: s += A[i] i += 1 return s
O(sqrt(n))
time complexity query.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With