算法复杂度思考

算法设计与分析_第1次作业

1. 时间复杂度与程序运行时间

请编写复杂度为 O ( n 2 ) {\rm{O}}(n^2) O(n2) O ( n log ⁡ n ) {\rm{O}}(n\log n) O(nlogn) O ( n ) {\rm{O}}(n) O(n)的任意程序,在不同问题规模下,记录运行时间,注明单位秒s或毫秒ms(写清运行代码的机器CPU配置,)。

CPU: Intel i7-7700HQ 2.80GHz

问题规模 n n n O ( n 2 ) {\rm{O}}(n^2) O(n2) O ( n log ⁡ n ) {\rm{O}}(n\log n) O(nlogn) O ( n ) {\rm{O}}(n) O(n)
1000 0.004s -
10000 0.307s -
20000 1.211s 0.0009942s
40000 4.878s 0.0010012s
100000 30.351s 0.0009941s
1000000 - 0.003s
10000000 - 0.025s
100000000 - 0.224s
1000000000 - 2.366s

O ( n 2 ) {\rm{O}}(n^2) O(n2)测试代码:

#include 
#include 

int main()
{
    int n;
    scanf("%d", &n);

    clock_t st = clock();
    long long sum = 0;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            ++sum;
    clock_t ft = clock();
    double dt = (ft - st) * 1.0 / CLOCKS_PER_SEC;

    printf("sum = %lld\n", sum);
    printf("duration: %f s\n", dt);
    
    return 0;
}

O ( n log ⁡ n ) {\rm{O}}(n\log n) O(nlogn)测试代码:


O ( n ) {\rm{O}}(n) O(n)测试代码:

#include 
#include 

int main()
{
    int n;
    scanf("%d", &n);

    clock_t st = clock();
    long long sum = 0;
    for (int i = 0; i < n; ++i)
            ++sum;
    clock_t ft = clock();
    double dt = (ft - st) * 1.0 / CLOCKS_PER_SEC;

    printf("sum = %lld\n", sum);
    printf("duration: %f s\n", dt);
    
    return 0;
}

2. 斐波那契数列计算

请用递归和递推实现斐波那契数列第 n n n项的计算,结果对1000000007取模。

Fibonacci numbers:
F 0 = 0 , F 1 = 1 F_0 = 0, F_1 = 1 F0=0,F1=1
F n = F n − 1 + F n − 2 , ( n ≥ 2 ) F_n = F_{n-1} + F_{n - 2},(n \geq 2) Fn=Fn1+Fn2,(n2)

n n n为多大时,递归计算已经明显变慢了?

  1. 递归:当n为40的时候,有轻微卡顿,n每上升1,都有一个时间更长的卡顿。

    • 猜测的原因:每次都要去求F(n),一个递归函数嵌套两个,如此嵌套。相当于 2 n 2^n 2n,每个步骤几乎都是重复的。若求f(3),其实再求f(4)的时候已经求过。
  2. 递推:上亿级别就需要卡顿一下。

    • 猜测的原因:递归是由上向下求,而递推是从一开始的值一直向着最终的结果进行求取。就是相当于普通的运算,因为再其中增加了取模运算,所以速度比只有普通的运算的程序要慢一些。线性时间求解。

代码:

#include 
#include 
#include 
using namespace std;
int F_1(int n)
{
    if (n < 1)
        return 0;
    else if (n == 1 || n == 2)
        return 1;
    return F_1(n - 1) + F_1(n - 2);
}

int F_2(int n)
{
    int a, b;
    a = 1, b = 1;
    for (int i = 3; i <= n; i++)
    {
        int t = b;
        b = (a + b) % 1000000007;
        a = t;
    }
    return b;
}
int main()
{
    int N;
    cin >> N;
    clock_t st = clock();
    cout << F_2(N);
    clock_t ft = clock();
    double dt = (ft - st) * 1.0 / CLOCKS_PER_SEC;

    printf("duration: %lf s\n", dt);
    return 0;
}

3. 最大子序和问题

请编写 O ( n 3 ) {\rm{O}}(n^3) O(n3) O ( n 2 ) {\rm{O}}(n^2) O(n2) O ( n log ⁡ n ) {\rm{O}}(n\log n) O(nlogn)的代码,自己生成一些测试数据进行本地测试,并分别在CG平台上提交。

代码:

  1. O ( n 3 ) {\rm{O}}(n^3) O(n3)
#include 

using namespace std;
int main()
{
    int N;
    scanf("%d", &N);
    int a[N];
    for (int i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);
    }
    long long maxt = -1000000;
    for (int i = 0; i < N; i++)
    {
        for (int j = i; j < N; j++)
        {
            long long sum = 0;
            for (int k = j; k < N; k++)
            {
                sum += a[k];
                maxt = max(maxt, sum);
            }
        }
    }
    printf("%d", maxt);
    return 0;
}
  1. O ( n 2 ) {\rm{O}}(n^2) O(n2)
#include 

using namespace std;
int main()
{
    int N;
    scanf("%d", &N);
    int a[N];
    for (int i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);
    }
    long long maxt = -1000000;
    for (int i = 0; i < N; i++)
    {
        long long sum = 0;
        for (int j = i ; j < N; j++)
        {
            sum += a[j];
            maxt = max(maxt, sum);
        }
    }
    printf("%d", maxt);
    return 0;
}
  1. O ( n 3 ) {\rm{O}}(n^3) O(n3)
#include 

using namespace std;
const int MAX_N = 1e5 + 5;
int a[MAX_N];
int solve(int L, int R)
{
    int mid = (L + R) / 2;
    if (L == R)
    {
        return a[L];
    }
    int maxL = solve(L, mid);
    int maxR = solve(mid + 1, R);
    int mL, mR, sL, sR;
    sL = sR = 0;
    mL = mR = -100000;
    for (int i = mid; i >= L; i--)
    {
        sL += a[i];
        mL = max(mL, sL);
    }
    for (int i = mid + 1; i <= R; i++)
    {
        sR += a[i];
        mR = max(sR, mR);
    }
    int maxmid = mR + mL;
    return max(maxmid, max(maxL, maxR));
}
int main()
{
    int N;
    scanf("%d", &N);
    for (int i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);
    }
    printf("%d", solve(0, N - 1));
    return 0;
}

你可能感兴趣的:(算法设计,算法,复杂度,算法导论)