Java时间复杂度和空间复杂度分析

1:实现二分查找算法的递归及非递归。(分析时间复杂度及空间复杂度)

迭代算法

#define _CRT_SECURE_NO_WARNINGS

#include
#include
#include

int BinarySearch(int arr[], int len, int num)
{
    assert(arr);

    int left = 0;
    int right = len - 1;
    int mid;

    while (left <= right)
    {
        mid = left + (right - left) / 2;

        if (num > arr[mid])
        {
            left = mid + 1;
        }
        else if (num < arr[mid])
        {
            right = mid - 1;
        }
        else
        {
            return mid;
        }
    }

    return -1;
}



int main()
{
    int arr[] = { 1,2,3,4,5,6,7,8,9 };
    int length = sizeof(arr) / sizeof(arr[0]);
    int aim = 9;
    int result;

    result = BinarySearch(arr, length, aim);

    if (result == -1)
    {
        printf("Can't find %d\n", aim);
    }
    else
    {
        printf("%d at %d postion\n", aim,result + 1);
    }


    return 0;
}

二分查找时,每次都在原有查找内容进行二分,所以时间复杂度为O(log2 n)
因为变量值创建一次,所以空间复杂度为O(1)

递归算法

int BinarySearchRecursion(int arr[5], int lef, int rig,int aim)
{
    int mid = lef + (rig - lef) / 2;


    if (lef <= rig)
    {
        if (aim < arr[mid])
        {
            rig = mid - 1;
            BinarySearchRecursion(arr, lef, rig, aim);
        }
        else if (arr[mid] < aim)
        {
            lef = mid + 1;
            BinarySearchRecursion(arr, lef, rig, aim);
        } 
        else if (aim == arr[mid])
        {
            return mid;
        }

    }
    else
        return -1;

}


int main()
{
    int arr[] = { 1,2,3,5,6, };
    int sz = sizeof(arr)/sizeof(arr[0]);
    int result;

    result = BinarySearchRecursion(arr, 0, sz - 1, 4);

    if (-1 == result)
    {
        printf("Can't find it.\n");
    }
    else
        printf("Aim at %d location\n", result+1);
}

时间复杂度为O(log2 n)
每进行一次递归都会创建变量,所以空间复杂度为O(log2 n)

2:实现斐波那契数列的递归及非递归。(分析时间复杂度及空间复杂度)

迭代算法

int FeiBoNaCciInteration(int a,int b,int num)
{
    int c;

    if (num <= 0)
        return -1;
    else if (num == 1)
        return a;
    else if (num == 2)
        return b;
    else
    {
        while (num - 2)
        {
            c = a + b;
            a = b;
            b = c;
            num--;
        }
        return c;
    }

}

int main()
{
    int n;
    int result;

    printf("Input n\n");
    scanf("%d", &n);

    result = FeiBoNaCciInteration(2, 3, n);//可自定义输入第一个数和第二个数
    if (result == -1)
    {
        printf("Input Error!\n");
    }
    else
    {
        printf("n is %d\n", result);
    }

    return 0;
}

时间复杂度O(n)
空间复杂度为O(1)

递归算法

int FeiBoNaCciRecursion(int num)
{
    if (num < 0)
        return -1;
    if (num <= 2 && num > 0)
        return 1;
    else
        return FeiBoNaCciRecursion(num - 1) + FeiBoNaCciRecursion(num - 2);

}

int main()
{
    int n;
    int result;

    printf("Input n\n");
    scanf("%d", &n);

    result = FeiBoNaCciRecursion(n);

    if (result == -1)
        printf("Input Error!\n");
    else
        printf("Result is %d\n", result);

    return 0;
}

时间复杂度为O(2^n)
空间复杂度为O(n)

 参考:https://blog.csdn.net/halotrriger/article/details/78994122?utm_source=copy

你可能感兴趣的:(数据结构)