Chapter 1
程式規格、設計、與分析


1.2 Running time analysis

<1> Time analysis:

Measure the speed/performance of a algorithm.

<2> Big-O notation: the order of algorithm.

  1. an algorithm is said th be O(Np)
    if its operation count Q satifies:
    where C is a constant.

    if p=1 the algorithm is called linear.
    if p=2 the algorithm is called quadratic.

  2. An algotithm s called logarithnic O(log n)
    if its operation count Q satifies:
    ex.
    count2n4n
    n2n4n
    n24n216n2
    log nlog n + log 2log n + log16

    Remark:

    1. The order of an algorithm generally is more important than the speed of the processor.
      ex.
      nO(n) / 3n O(n2) / n2+2n O(log n) / log n + 1
      10301202
      1000300102003
      10000300010020004
    2. A loop that does a fixed amount of operations n times requires O(n) time.
      eg.
      for ( i = 0 ; i < n ; i++ )
          j = j + 1 ;
      
    3. worst-case, average-case, best-case analysis.
      worst case
      Count the number of operations inaverage case
      bestt case