2. Algorithm Analysis
2.1. Algorithm Analysis
For each of the following six programs:
Give an analysis of the running time (Big-Oh).
Implement and give the running time for several values of \(N\).
Compare your analysis with the actual running time (plot the results).
1sum = 0;
2for (i = 0; i < n; i++){
3 sum++;
4}
1sum = 0;
2for (i = 0; i < n; i++){
3 for (j = 0; j < n; j++){
4 sum++;
5 }
6}
1sum = 0;
2for (i = 0; i < n; i++){
3 for (j = 0; j < n * n; j++){
4 sum++;
5 }
6}
1sum = 0;
2for (i = 0; i < n; i++){
3 for (j = 0; j < i; j++){
4 sum++;
5 }
6}
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}
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: