2. Algorithm Analysis

2.1. Algorithm Analysis

For each of the following six programs:

  1. Give an analysis of the running time (Big-Oh).

  2. Implement and give the running time for several values of \(N\).

  3. Compare your analysis with the actual running time (plot the results).

alg 1
1sum = 0;
2for (i = 0; i < n; i++){
3    sum++;
4}
alg 2
1sum = 0;
2for (i = 0; i < n; i++){
3    for (j = 0; j < n; j++){
4        sum++;
5    }
6}
alg 3
1sum = 0;
2for (i = 0; i < n; i++){
3    for (j = 0; j < n * n; j++){
4        sum++;
5    }
6}
alg 4
1sum = 0;
2for (i = 0; i < n; i++){
3    for (j = 0; j < i; j++){
4        sum++;
5    }
6}
alg 5
1sum = 0;
2for (i = 0; i < n; i++){
3    for (j = 0; j < i*i; j++){
4        for (k = 0; k < j; k++){
5            sum++;
6        }
7    }
8}
alg 6
 1sum = 0;
 2for (i = 0; i < n; i++){
 3    for (j = 0; j < i*i; j++){
 4        if (j % i == 0){
 5            for (k = 0; k < j; k++){
 6                sum++;
 7            }
 8        }
 9    }
10}

2.2. LeetCode

If you are done you can start doing the following problems on LeetCode: