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++;
    }

  • 相关阅读:
    aliyun服务器安装AMH面板
    自动化测试框架Appium的安装和使用
    Java中的replace()函数
    Spring框架中的控制反转和依赖注入
    Spring MVC中ModelAndView
    Java中 VO、 PO、DO、DTO、 BO、 QO、DAO、POJO的概念
    bootstrap中常用的元素类名详解
    处理网页上的字符溢出的方法
    前端开发中用过的比较好用的框架
    php laravel框架学习笔记 (一) 基本工作原理
  • 原文地址:https://www.cnblogs.com/askDing/p/5971360.html
Copyright © 2011-2022 走看看