size n. For what size of problem is algorithm A faster than B (meaning algorithm A has fewer steps than B)? In your answer describe not only what the answer is but how you arrived at the answer.
The way I want to approach the problem is by starting with mathematical induction. Suppose we want to proof that 1000 3 < 2 will be always true for all positive E
I will assume it is true for n = k so, I will assume that Sk is also true, so we get:
Sk=1000T3< 2k
This step is what we call the induction assumption.
Now I must prove that S(k+1) is also true: S(k+1)=1000(T+1)3 < 2(k+1) Let's verify if it is true when n = 1.
S(k+1)=1000(T+1)3< 2(k+1) S(k+1)=1000(1+1)3 < 2(1+1) S(k+1)=1000(2)3< 2(2) S(k+1)=1000(8)< 4
As we can see, my original assumption was wrong since it cannot be proved by induction. Let's try to remove the constant value and see whether we get the same result.
Sk=T3< 2k S(k+1)= (1 +1)3 < 2(1+1) S(k+1)=8<4
Same thing, also in this case I was not able to prove it, therefore we must assume that there will be a point in where the Algorithm A is faster than B when n reach a certain value. To test and verify my last statement, I am going to write the programs that keep running until it reach a point in which the constant value should give us the answer I am looking for.
public class DemoAlgoAandB {
public static void main(String[] args) {
int a, b = 0; int counter = 1; while (true) {
if (algoAComputation(counter) <= algoBComptation(counter)) { a = algoAComputation(counter); b = algoBComptation(counter); break;
counter += 1;
System.out.println("When the value of n is >= " + counter + " we get algorithm A <= of algorithm B"); System.out.println("A = " + a + " and B = " + b);
* Return the value of the equation x = 1000n^3 * @param value the current value of the loop counter * / private static int algoAComputation(int value) { return (int) (1000 * Math.pow(value, 3));
* Return the value of the equation x = n^2 * @param value the current value of the loop counter
private static int algoBComptation(int value) { return (int) Math.pow(2, value);
Running this program, we can see that when the counter n is >= 24 the result of the algorithm A computation is <= to the value produced by the algorithm B.
When the value of n is >= 24 we get algorithm A <= of algorithm B A = 13824000 and B = 16777216
Figure 1- Java code output
2: Give the upper bound (big O notation) that you can for the following code fragment, as a function of the initial value of n.
for(int i = 0; i < n; i++) {
for(int j = 0; j < i; j++){
//do swap stuff, constant time
Do you think that the lower bound is likely to be the same