读书笔记: 时间复杂性(步数) Time Complexity - Steps

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#define MAX_SIZE 10

int count = 0;

int main (int argc, const char * argv[])
{
    return 0;
}

//total 2n+3
float iteration_sum(float list[],int n){
    float tempsum = 0;count++;  //for assignmnet 
    int i;
    for(i=0;i<n;i++){
        count++; //for the for loop
        tempsum += list[i]; count++; //for assignment
    }
    count++; //last execution of for
    count++; //for return 
    return tempsum;
}

//total 2n+2
float recursion_sum(float list[],int n){
    count++; //for if condition
    if(n){
        count++; //for return and rsum invocation 
        return recursion_sum(list, n-1) + list[n-1];
    }
    count++;
    return list[0];
}

//带计数的矩阵加法
//count 最终将是2rows*cols + 2rows + 1,应将矩阵按列的顺序相加。
void add(int a[][MAX_SIZE],int b[][MAX_SIZE],int c[][MAX_SIZE],int rows,int cols){
    int i,j;
    for(i=0;i<rows;i++){
        count++; //for i for loop
        for(j=0;j<cols;j++){
            count++; //for j for loop
            c[i][j]=a[i][j]+b[i][j];
            count++; //for assignment statement
        }
        count++; //last time of j for loop
    }
    count++; //last time of i for loop
}