The number of operations executed by algorithms A and B is 8
The number of operations executed by algorithms A and B is 8nlogn and 2n^2, respectively. Determine n_0 such that A is better than B for n greaterthanorequalto n_0.
Solution
The result is 44.
So how do you solve such problems?
The best way to answer such problem is to write a code in a choice of your language. I wrote a code in C as given below:
#include<stdio.h>
#include<math.h>
int main() {
int n=2;
double A(int), B(int);
while (A(n) >= B(n))
n++;
printf (\"n = %d\ \",n);
}
double A(int n) {
double res = 8*n*log(n)/log(2);
//printf(\"A(%d) = %lf\ \", n, res);
return res;
}
double B(int n) {double res = n*n;
//printf(\"B(%d) = %lf\ \", n, res);
return res;
}
and the output was 44.
(Note that we have considered log of base 2)
(For log base 10 the answer would be 7)
(For log base e the answer would be 27)
(For log base 2 the answer would be 44)
