zoukankan      html  css  js  c++  java
  • 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++;
    }

  • 相关阅读:
    js将UTC时间转化为当地时区时间 用JS将指定时间转化成用户当地时区的时间
    elementUI里面,用tabs组件导致浏览器卡死的问题
    根据数组对象中的属性值删除对象
    js货币金额正则表达式
    vue elementui input不能输入的问题
    vue+elementui--$message提示框被dialog遮罩层挡住问题解决
    Oracle日期函数
    plsql查询报错:Dynamic Performamnce Tables not accessible
    Oracle rownum和rowid的区别
    Oracle通过序列实现主键自增长
  • 原文地址:https://www.cnblogs.com/askDing/p/5971360.html
Copyright © 2011-2022 走看看