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

  • 相关阅读:
    Introduction to Machine Learning
    IEEE 802.3 Ethernet
    Introduction to Computer Networks(网络架构与七层参考模式)
    Integral类型的跨平台使用
    Aggregate类型以及值初始化
    合成的默认构造函数定义为delete的一种情况(针对C++11标准)
    版本控制工具Git
    编程实现计算器
    Linux客户/服务器程序设计范式2——并发服务器(进程池)
    Linux客户/服务器程序设计范式1——并发服务器(多进程)
  • 原文地址:https://www.cnblogs.com/askDing/p/5971360.html
Copyright © 2011-2022 走看看