Skip to content

Instantly share code, notes, and snippets.

@mvoitko
Last active October 7, 2019 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvoitko/dfa18c782092912608ff7a4019bbf95b to your computer and use it in GitHub Desktop.
Save mvoitko/dfa18c782092912608ff7a4019bbf95b to your computer and use it in GitHub Desktop.
Find the number of times f() is called as a function of the input size n.
a. for (i = n; i > 1; i /= 2)
f()
a. O(log n)
b. for (i = 0; i * i < n; i++)
f()
b. O(sqrt n)
c. for (i = 0; i < n; i++)
for (j = 0; j < 256; j++)
f()
c. O(n)
d. for (i = 1; i <= n * n - 10; i++)
for (j = 1; j <= i; j ++)
f()
d. O(n**4)
e. for (i = 1; i <= n; i++)
for (j = 1; j <= i; j++)
for (k = 1; k <= j; k++)
f()
e. O(n**3)
f. for (i = 1; i <= n; i++)
for (j = 1; j < i; j *= 2)
f()
f. O(logn!) = O(nlgn)
g. for (i = 1; i <= n; i++)
for (j = 1; j <= n; j += i)
f()
g. O(n*lnn)
h. void compute(int n)
if (n == 0) return;
for (int i = 0; i < n; i++)
f()
compute(n/2)
compute(n/2)
h. O(n*logn)
i. for (int i = 8; i < n + 7; i += 2) { f(); }
f() is called (n + 7 − 8)/2 times, which is in the order of n.
j. for (int i = 8; i < n + 7; i *= 3) { f(); }
8 * 3**i >= n + 7 => f is called log3 (n+7)/8 times
j. for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j)
f();
for (int j = 1; j < n; j *= 2)
f();
}
n*(n + logn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment