• Examples of complexity pattern


    O(1):constant - the operation doesn't depend on the size of its input, e.g. adding a node to the tail of a linked list where we always maintain a pointer to the tail node.
    int i=0;
    i++;
    ++i;
    i+=6;
    O(n):linear - the run time complexity is proportionate to the size of n.
    int i,n=100,s=0;
    for(i=0;i<n;i++)
    {
    s+=1;
    }
    O(n2):quadratic - the run time complexity is proportionate to the square of size of n, e.g., bubble sort.
    int i,j,n=100,s=0;
    for(i=0;i<n;i++)
    {
    for(j=0;j<n;j++)
    {
    s+=1;
    }

    }
    O(n3):cubic - very rare.
    int i,j,k,n=100,s=0;
    for(i=0;i<n;i++)
    {
    for(j=0;j<n;j++)
    {
    for(k=0;k<n;k++)
    {
    s+=1;
    }
    }
    }
    O(logmn): logarithmic: normally associated with algorithms that break the problem into smaller chunks per each invocation, e.g. searching a binary search tree.
    int i,n=100,m=2; /* m could be any number, e.g.,2,10 */
    for(i=0;i<n;i++)
    {
    i=i*m;
    }
    O(nlogn): just nlogn: usually associated with an algorithm that breaks the problem into smaller chunks per each invocation, and then takes the results of these smaller chunks and stitches them back together, e.g. quick sort.
    int i,n=100;

    int m_expo(int m)
    {
    /* an auxilary function that return the value
    of m to the mth exponential, not included to the
    time consumation*/

    int k = m;
    for(j=1;j<m;j++)
    {
    /* 2 could also be other number */
    k = k * k;
    }
    return k;
    }

    /* this is the part whose consumation is O(nlogn) */
    for(i=0;i<m_expo(n);i++)
    {
    /* 10 could be other number */
    i=i*10;
    }
    O(n1/2): square root.
    int i,n=100;
    while(i*i<n)
    {
    i++;
    }
    O(2n):exponential - incredibly rare.
    int i,n=100;
    int expo(int m)
    {
    /* an auxilary function that return the value
    of 2 to the mth exponential, not included to the
    time consumation*/

    int k =1;
    for(j=1;j<m;j++)
    {
    /* 2 could also be other number */
    k = k * 2;
    }
    return k;
    }

    /* this is the part whose consumation is O(2n)) */
    while(i<expo(n))
    {
    i++;
    }
    O(n!):factorial - incredibly rare.
    int i,n=100;
    int factorial(int m)
    {
    /* an auxilary function that return the
    factorial value of m */

    int k =1;
    for(j=1;j<=m;j++)
    {
    /* 2 could also be other number */
    k = j * k;
    }
    return k;
    }

    /* this is the part whose consumation is O(n!) */
    while(i<factorial(n))
    {
    i++;
    }
    O(nn):not exist in real life.
    int i,n=100;
    int mm_expo(int m)
    {
    /* an auxilary function that return the value
    of m to the mth exponential, not included to the
    time consumation*/

    int k = m;
    for(j=1;j<m;j++)
    {
    k = k * m;
    }
    return k;
    }

    /* this is the part whose consumation is O(nn)) */
    while(i<mm_expo(n))
    {
    i++;
    }

  • 相关阅读:
    解决png图像透明问题
    常用的CSS命名规则
    站点跨域登录
    SVN服务器配置
    开源方便的PHP & Flash图表:Open Flash Chart
    php+mysql无限级分类(非递归)
    地址栏显示图标 Shortcut Icon
    mysql set类型和enum类型
    通过 WebDAV 协议访问版本库(http://)
    年薪第一的数据库工程师是怎样炼成的
  • 原文地址:https://www.cnblogs.com/askDing/p/5971360.html
Copyright © 2020-2023  润新知